Giter Site home page Giter Site logo

amezin / vscode-linux-kernel Goto Github PK

View Code? Open in Web Editor NEW
533.0 22.0 152.0 9 KB

Visual Studio Code project/compile_commands.json generator for Linux kernel sources and out-of-tree modules

License: The Unlicense

Python 100.00%
visual-studio-code linux-kernel vscode kernel

vscode-linux-kernel's Introduction

Visual Studio Code project for Linux kernel sources

Ensure the kernel is built (at least, all *.cmd files should be generated):

$ make defconfig
$ make

Clone this repository as ".vscode":

$ git clone [email protected]:amezin/vscode-linux-kernel.git .vscode

Generate compile_commands.json:

$ python .vscode/generate_compdb.py

If you are not compiling kernel for x64, change intelliSenseMode in c_cpp_properties.json. Possible values as of vscode-cpptools 1.0.1:

  • gcc-x86
  • gcc-x64
  • gcc-arm
  • gcc-arm64

Open the project:

$ code .

Out-of-tree builds

#4

Kernel can be built with separate output directory:

$ make O=../linux-build defconfig
$ make O=../linux-build

In this case, you should pass the directory to generate_compdb.py:

$ python .vscode/generate_compdb.py -O ../linux-build

compile_commands.json will still be generated in the current directory (root of the linux repository). Unfortunately, tasks.json will not work out of the box in this configuration (TODO).

Out-of-tree module development

If you build your module with this command:

$ make -C $KDIR M=$PWD modules

You could generate compile_commands.json with:

$ python .vscode/generate_compdb.py -O $KDIR $PWD

Example: https://github.com/amezin/nzxt-rgb-fan-controller-dkms

vscode-linux-kernel's People

Contributors

amezin avatar im-0 avatar jamikettunen avatar nunojsa avatar zehortigoza avatar zonque avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vscode-linux-kernel's Issues

Re-generate compile_commands.json incrementally

Generate a .json file for every .cmd.* file, then join them. So the Python script will only handle one .cmd.* at a time, and a Makefile will run the script for all files.

  1. .json files for unchanged .cmd files won't be regenerated needlessly.
  2. make's globbing/wildcard is probably faster than Python's one.
  3. this will be closer to something that can be integrated into Kbuild
  4. Probably it'll be possible to trick kernel's Makefile to include/recursively call this Makefile (and thus automatically pass correct O=..., for example)

does't work with 2.6.11.10 kernel

parse_cmd_file with ./kernel/.kmod.o.cmd results:

command match result:
{'kernel/kmod.o': 'gcc -Wp,-MD,kernel/.kmod.o.d -nostdinc -isystem /usr/lib/gcc-lib/i486-linux/3.3.5/include -D__KERNEL__ -Iinclude -Wall -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -ffreestanding -O2 -fomit-frame-pointer -pipe -msoft-float -mpreferred-stack-boundary=2 -march=i686 -Iinclude/asm-i386/mach-default -DKBUILD_BASENAME=kmod -DKBUILD_MODNAME=kmod -c -o kernel/kmod.o kernel/kmod.c'}

source match result:
{}

Poor documentation

  1. OK, I've generated compile_commands.json, but what's next? How to use it? I expect it should be another way to build module/kernel from VS Code rather than running 'make' in terminal, shouldn't it? No clue.

  2. Also there is a statement in README:
    "Unfortunately, tasks.json will not work out of the box in this configuration"
    And how to make it work manually? What is workaround?

Please, add more instructions to README.

Doesn't work for out-of-tree builds

For instance, during Android Linux kernel development, the builds are usually done by a make bootimage is the global repo-workspace. (repo is an Android-specific tool for managing multiple git repositories) This builds the kernel with proper flags and configuration. This affects the paths. For instance:

repo workspace: /path/to/lineage-src
kernel repository and the VS Code workspace: /path/to/lineage-src/kernel/fxtec/msm8998
build artifacts: /path/to/lineage-src/out/target/product/pro1/obj/KERNEL_OBJ/usr/.initramfs_data.cpio.gz.cmd

However, the python script expects the cmd files to be present in the current working directory tree. One can change directory to point to some build artifact directory, but then the relative paths are incorrect. Trying to correct them manually causes some include path errors.

Out of tree builds are nothing new for the Linux kernel and are not Android-specific: https://lwn.net/Articles/90818/


Note: I tried playing around with includePath but it after arriving at:

            "includePath": [
                "${workspaceFolder}/include",
                "${workspaceFolder}/arch/arm64/include",
                "/path/to/lineage/out/target/product/pro1/obj/KERNEL_OBJ/arch/arm64/include/",
                "/path/to/lineage/out/target/product/pro1/obj/KERNEL_OBJ/arch/arm64/include/generated",
                "/path/to/lineage/out/target/product/pro1/obj/KERNEL_OBJ/include/",
                "/path/to/lineage/out/soong/ndk/sysroot/usr/include"
            ]

I ended up having errors like function type is not allowed for bool inside a definition of a struct, so something is completely wrong here


To reproduce, follow https://wiki.lineageos.org/devices/pro1/build, instead of the Start the build step, execute make bootimage in the root of the LineageOS tree. You can probably use some different device, the outcome should be the same.

Here's my compile commands, both vanilla and with paths adjusted.

compile_commands.json.zip
compile_commands_patched.json.zip


EDIT: the following contents of c_cpp_properties.json worked for me:

{
    "configurations": [
        {
            "name": "Linux",
            "cStandard": "c11",
            "intelliSenseMode": "gcc-x64",
            "compileCommands": "${workspaceFolder}/compile_commands.json",
            "defines": ["__KERNEL__"],
            "includePath": [
                "${workspaceFolder}/include",
                "${workspaceFolder}/arch/arm64/include",
                "/marcin/proj/lineage/out/target/product/pro1/obj/KERNEL_OBJ/arch/arm64/include/",
                "/marcin/proj/lineage/out/target/product/pro1/obj/KERNEL_OBJ/arch/arm64/include/generated",
                "/marcin/proj/lineage/out/target/product/pro1/obj/KERNEL_OBJ/include/",
                "/marcin/proj/lineage/out/soong/ndk/sysroot/usr/include"
            ]
        }
    ],
    "version": 4
}

Licensing

Hi amezin,
Your vscode project for the linux kernel has been helpful to me. Thanks!

Could you please clarify what license this tool is distributed under, if you have the time?

Thanks!

Feature Request: add build needed soft link files

When code linux-3.17.28, VS code loads all the kernel files (30000+).This will cause VS Code analysis to be slow, and there are many files in it that I don't care about, I only care about those files that are needed for compilation (6000+).
I think it is possible to create a folder outside the kernel (for example, linux-3.17.28-build-need) with soft links to the files needed for compilation. and then code linux-3.17.28-build-need. This will greatly shorten the time of vscode analysis

Fails to create compile_commands for out of tree module

Summary

Generating a compile_commands.json file for kernel module development per the README throws an error ValueError: Chunksize must be 1+, not 0.

Steps to reproduce

  1. Clone the kernel I'm using from https://git.archlinux.org/linux.git (you can probably just use whichever you want but I'm trying to be exhaustive here)
  2. Check out the tag I'm using with git checkout v5.11.13-arch1
  3. Use default config make O=$BUILDDIR defconfig (I'm on x86_64)
  4. Add local version if you'd like (I did this)
  5. Build kernel cd $BUILDDIR && make -j8
  6. Clone hello world kernel module (from LDD3) somewhere else git clone https://github.com/jpear1/hello-module.git
  7. Observe this module builds normally with make -C $BUILDDIR M=$PWD modules. Optionally make clean after.
  8. Clone vscode-linux-kernel per README: git clone [email protected]:amezin/vscode-linux-kernel.git .vscode
  9. Attempt to generate compile_commands per README: python .vscode/generate_compdb.py -O $BUILDDIR $PWD
  10. Observe error:
Building *.o.cmd file list...
Parsing *.o.cmd files...
Traceback (most recent call last):------------------] 0.0%
  File "/home/jack/scratch/hello/.vscode/generate_compdb.py", line 88, in <module>
    main()
  File "/home/jack/scratch/hello/.vscode/generate_compdb.py", line 84, in main
    gen_compile_commands(**vars(cmd_parser.parse_args()))
  File "/home/jack/scratch/hello/.vscode/generate_compdb.py", line 64, in gen_compile_commands
    for compdb_chunk in pool.imap_unordered(functools.partial(parse_cmd_file, out_dir), cmd_files, chunksize=int(math.sqrt(len(cmd_files)))):
  File "/usr/lib/python3.9/multiprocessing/pool.py", line 437, in imap_unordered
    raise ValueError(
ValueError: Chunksize must be 1+, not 0

Other info

Python version: 3.9.3
GNU Make version: 4.3
GCC version: 10.2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.