reddit / emacs

Help a Linux kernel dev setup LSP

SEgopher · 2022-10-18 23:08
I am a FTE Linux kernel dev, I use bare bones Emacs and have been wondering if maybe I should add LSP support, as I hear it's technically possible to use a LS with the kernel. Anyone do this today, or have advice on how to configure Eglot for the kernel? All I have ever used is cscope and ctags. I'd like to get it working so that the LSP server picks up the correct headers for the build arch and picks up all the GCC flags. I also usually run my builds inside of a container so I don't use my system's gcc toolchain. I am not an Emacs wizard, so detailed instructions for an idiot are most welcome.
Faerryn · 2022-10-18 23:42
Look into https://github.com/rizsotto/Bear, https://clangd.llvm.org/, and eglot/lsp-mode. You should be able to generate a `compile-command.json` after doing a clean build of the kernel, and use `eglot` to start using LSP.
inglourious_basterd · 2022-10-18 23:47
(What's an "FTE" kernel dev? Googling this returns a lot of pages that use the acronym but none that define it.)
Galizur_HaMalakh · 2022-10-18 23:57
The first step is choosing whether to use the [eglot](https://github.com/joaotavora/eglot) or [lsp](https://emacs-lsp.github.io/lsp-mode/) package for your LSP client. After choosing this, you will need an actual LSP server installed. The two main (and best) contenders are [ccls](https://github.com/MaskRay/ccls) and [clangd](https://clangd.llvm.org/). For both the client and the server, the choice you make is personal, and it's quite easy to switch between them, considering a simple configuration. Both servers need a compilation database `compile_commands.json` in order to populate and read symbols and locations, as well as flags and configurations. Some build generators like CMake and Meson are able to produce these files (`cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=1` for the first and automatically for the latter). If you aren't using such a system and using, for example, plain Makefiles, you will need a tool like [Build EAR](https://github.com/rizsotto/Bear) to produce the compilation database. As for using these tools inside a container, the ccls [wiki](https://github.com/MaskRay/ccls/wiki/Project-Setup) has a section on how to set them up. As for the Emacs configuration, you should only need a few lines of code, in order to get them up and running, if you are planning to keep your setup as close to vanilla as possible. If you are interested, you could additionally install a completion framework like [company-mode](https://company-mode.github.io/) to help you out.
SEgopher · 2022-10-19 00:40
Full time employee
electricity-wizard · 2022-10-19 01:35
also a linux kernel dev using emacs. I use LSP mode with clangd. The process on setting it up is really easy! go to your Linux kernel directory ``` make CC=clang defconfig make CC=clang ``` once the kernel is compiled simply ``` scripts/clang-tools/gen_compile_commands.py ``` which will create a compile\_commands.json which LSP mode will automatically find. then you're good to go. edit: it also works with cross compiling just ``` make ARCH=arm64 CC=clang CROSS_COMPILE=aarch64-linux-gnu- ``` I used to use cscope too and once I tried LSP mode I never looked back
hangingpawns · 2022-10-19 05:24
Several solutions have been posted now. Do any of them sound reasonable to you? You absolutely can use LSP mode on kernel code. The key is to get the compile commands json. You can get that via a tool called bear, which you can add to your container, or you can get it via cmake.
Thaodan · 2022-10-19 12:41
You don't even need to change the compiler to clang run gen\_compiler\_commands.py. What also works instead of the script is to use bear.
yep808 · 2022-10-19 21:26
I wrote a blog on Emacs and LSP a while back, hope it helps! https://ianyepan.github.io/posts/emacs-ide/
SEgopher · 2022-10-19 21:59
Thanks, I appreciate it! Do you normally get some compiler warnings at the top of the file, or are you completely warning free? By the way, - what snippet are you using to auto-format with according to the check-patch guidelines? - do you have any snippets for sending patches within emacs, I embarrassingly use mutt and git send-email for that. Any other tools or snippets you want to share is most appreciated, like I said my setup is very basic.
SEgopher · 2022-10-19 22:01
In do you prefer out of ccls and clangd, does one support gcc extensions better than the other?
electricity-wizard · 2022-10-20 01:35
Glad I can help. 1. Do I get compiler warnings at the top of the file? Not usually, you can always do `M-x flymake-goto-next-error` and it will tell you whats wrong. 2. What snippets do I use to auto-format with according to check patch guidelines If I understand you correctly, I simply put this in my init.el (setq c-default-style "linux") This has done a pretty good job for me. 3. Do I have snippets for sending patches because you're embarrassed about mutt first thing, mutt is good! but give mu4e a try. I use magit for all my patch formatting, the only thing it doesn't have is git send-email. But you can run git commands in magit. 4. any tips or tricks? Yes! use a [.dir-locals.el](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html) put it in the directory you're working in and change the compile-command so you can just hit `M-x compile` and it will use the compile command you give it.
Galizur_HaMalakh · 2022-10-20 15:54
I personally prefer clangd, only because the completion recommendations are more up my alley. AFAIK, they both run best with clang, but in the end of the day, your compiler is what drives the server, not the other way round. So using gcc, you'll get the most from its native extensions. There is also the customization point. Clangd is pretty much install and start coding, while ccls might need a bit of tinkering. Clangd is simpler, while ccls is feature rich. I do not need all the features of ccls, I only have a global clangd config that sets my default version to C++20 and the relative path to my build directory, since I use OOT building, and a per-project config that sets some default flags in order to avoid useless diagnostics. In the end, it's just a matter of preference. I'd start with clangd as I find it easier and simpler, and maybe switch if my needs surpass what it can provide. As an extra tidbit of information, you can specify in your Emacs' config file the load order of the servers, so you can have both installed and only need to change a couple of variables to use the other.
bruchieOP · 2022-10-20 21:28
I know the obvious answers is for C dev, but if you plan to do rust dev drivers you may want to look over rust-analyzer and rustic...