# Local development of kernels

## Introduction

`kernel-builder` builds kernels in a sandbox. This has various benefits,
such as building kernels for a wide range of Torch versions, compatibility
with older C library versions and avoiding accidental dependencies.

However, this is not ideal during kernel development, since language
servers and IDEs do not interpret the `build.toml` file. As a result,
code completion will typically not work. `kernel-builder` provides the
`build2cmake` utility to generate CMake files to build native code and
setuptools files for building the kernel as a regular Python package.
Since CMake and setuptools are widely supported by IDEs, this provides
a much-improved development experience.

## Installing `build2cmake`

`build2cmake` is available as a Rust crate. After [installing Rust](https://rustup.rs),
it can be built and installed as follows:

```bash
$ cargo install build2cmake
```

## Generating a Python project with `build2cmake`

`build2cmake` generates a CMake/Python project from a [`build.toml`](./writing-kernels)
file. The invocation is as follows:

```bash
$ build2cmake generate-torch build.toml -f
```

The `-f` flag is optional and instructs `build2cmake` to overwrite
existing files.

It is recommended to do an editable install of the generated project into
your Python virtual environment for development:

```bash
$ pip install wheel # Needed once to enable bdist_wheel.
$ pip install --no-build-isolation -e .
```

**Warnings:**

- Kernels built in this way should **not** be published on the Kernel
  Hub. They do not fulfill the [kernel requirements](../kernel-requirements).
- Do not add the generated files to Git. `build2cmake` has regular updates
  and you generally want to use files generated by the latest version.

