> ## Documentation Index
> Fetch the complete documentation index at: https://docs.t3gemstone.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Building

> Building the Camera ISP SDK package on the host computer

> [!TIP]
>
> By the end of this section, you will have learned about the following topics.
>
> * Setting up the build environment with Devbox and Distrobox
> * Producing the DCC files, overlays and the installation package with Task
> * Cross-building the drivers of sensors whose driver is not in the kernel

The build produces the package to install on the board: the sensor descriptors are checked, the ISP tuning (DCC)
files and device tree overlays are compiled, and everything is packed into a single `.tar.gz` file. TI's
GStreamer plugin is built on the board itself; that step is described in [Installation](https://docs.t3gemstone.org/en/boards/o1/isp/installation.md).

## 1. Getting the Source Code

```bash theme={"system"}
git clone https://github.com/t3gemstone/camera-isp-sdk.git
cd camera-isp-sdk
```

## 2. Setting Up the Build Environment

As in the [SDK](https://docs.t3gemstone.org/en/sdk/introduction.md), every tool the build needs comes with a ready-made build environment.
Your computer only needs Docker and Devbox.

**1. Installing the Requirements**

The `setup.sh` script installs Docker and Devbox.

```bash theme={"system"}
./setup.sh
```

> [!WARNING]
>
> If Docker is installed for the first time, you need to log out and log back in.

**2. Activating the Devbox Shell**

Devbox installs Distrobox and Task.

```bash theme={"system"}
devbox shell
```

**3. Entering the Build Environment**

On its first run `task box` builds the Docker image, then enters the Distrobox environment.

```bash theme={"system"}
task box
```

On the first entry, `⚠️  First time user password setup ⚠️` asks you to set a password for the environment.
Once you see the `🚀 distrobox:workdir>` prompt, you are ready to build.

> [!NOTE]
>
> * In the environment the repository is `/home/workdir`; other directories of your computer are under `/run/host`.
> * The environment's home directory is the repository's `.distrobox/` folder; the TI tools it downloads are kept there.
> * `task destroy` removes the environment and its Docker image.

## 3. Tasks

All commands below run inside the build environment (`task box`). `task` without arguments lists all tasks and
the current settings.

```bash Terminal theme={"system"}
task
```

```bash Output theme={"system"}
task: Available tasks for this project:
* box:                   Enter Distrobox's Ubuntu 22.04 image that has the build tools
* build:                 Validate sensors, build DCC binaries and overlays
* check-overlays:        Apply the overlays to BASE_DTB=<dtb> with fdtoverlay
* clean:                 Remove the build directory
* dcc:                   Build DCC binaries (downloads TI's dcc_gen_linux once, or DCC_TOOLS_FROM=<file> installs one downloaded by hand)
* default:
* destroy:               Destroy the Distrobox container and the Docker image of the build environment
* drivers:               Cross-build the drivers the kernel lacks (sensors/<id>/driver) against KDIR=<kernel build dir>
* overlays:              Build device tree overlays
* package:               Build, then pack the tarball to unpack over / on the board (build/camera-isp-sdk-<version>-<board>.tar.gz)
* test:                  Unit tests (Python tool and GStreamer descriptor parser)
* validate:              Check descriptors and DCC sources against the board
* gst:check-patch:       Check that the plugin patch applies to TI's tree at the pinned commit
* gst:test:              Descriptor parser unit tests, loading every shipped sensor
* tools:test:            Unit tests of the camera-isp-sdk tool

Environment:

  BOARD          = 't3-gem-o1'
  SENSORS        = ''
  BUILD_DIR      = 'build'
  DCC_TOOLS_FROM = ''
  KDIR           = ''
  ARCH           = 'arm64'
  CROSS_COMPILE  = 'aarch64-linux-gnu-'
```

Settings follow the task name as `VARIABLE=value`.

| Variable | Default | Description |
| - | - | - |
| `SENSORS` | (all) | Sensors the task works on, e.g. `SENSORS="imx477 ov5647"` |
| `BOARD` | `t3-gem-o1` | Board description under `boards/` |
| `BUILD_DIR` | `build` | Output directory |
| `DCC_TOOLS_FROM` | | A `dcc_gen_linux` downloaded by hand |
| `KDIR`, `ARCH`, `CROSS_COMPILE` | `arm64`, `aarch64-linux-gnu-` | For building the sensor drivers (`task drivers`) |

## 4. Building the Package

```bash theme={"system"}
task build      # check the descriptors, build the DCC files and overlays
task package    # -> build/camera-isp-sdk-0.1.0-t3-gem-o1.tar.gz
```

You can use the `SENSORS` variable to put only some sensors into the package.

```bash theme={"system"}
task package SENSORS=ov5647
```

> [!NOTE]
>
> You need to build the drivers of sensors whose driver is not in the kernel first
> ([Building Sensor Drivers](https://docs.t3gemstone.org/en/boards/o1/isp/build.md#6-building-sensor-drivers)): the package carries the built
> drivers and leaves out a driver that was not built.

`task build` runs the `validate`, `dcc` and `overlays` tasks in that order. The outputs are under `build/`:

| Directory | Contents |
| - | - |
| `build/dcc/<sensor>/` | `dcc_viss.bin`, `dcc_2a.bin` and `dcc_ldc.bin` if the sensor has one |
| `build/overlays/` | `k3-am67a-t3-gem-o1-<port>-<sensor>.dtbo` for every sensor and port |
| `build/camera-isp-sdk-<version>-t3-gem-o1.tar.gz` | The package to install on the board |

## 5. TI's DCC Tools

The ISP tuning files are compiled from XML sources with TI's `dcc_gen_linux`. On its first run, `task dcc`
downloads it from the pinned `REL.IMAGING.11.00.00.00` tag of TI's imaging repository and checks its SHA-256.
The tool is not added to the repository; it is kept in `~/.cache/camera-isp-sdk/dcc-tools/`.

> [!WARNING]
>
> git.ti.com is sometimes unavailable for a while (HTTP 502/503). The download is retried; if it still fails,
> you can download the file with a browser and install it by hand. The checksum is checked either way.
>
> ```bash theme={"system"}
> task dcc DCC_TOOLS_FROM=~/Downloads/dcc_gen_linux
> ```
>
> `DCC_TOOLS_FROM` can also point to an imaging checkout or a Processor SDK RTOS directory.

## 6. Building Sensor Drivers

A sensor whose driver is not in the T3 Gemstone kernel (IMX477 today) carries it in `sensors/<sensor>/driver/`: the driver
sources and a `Kbuild` file. `task drivers` cross-builds the drivers of all such sensors, or of the sensors in
`SENSORS`, against a kernel build directory configured for the board's kernel. Sensors whose driver is in the
kernel are skipped. Building the kernel is described in [Kernel](https://docs.t3gemstone.org/en/sdk/kernel.md). A kernel directory outside the
repository shows up under `/run/host` in the environment.

```bash theme={"system"}
task drivers KDIR=/run/host/path/to/kernel-build
```

The modules (`.ko`) are built next to their sources. `task package` puts them into the package, which installs
them under `/lib/modules/<kernel>/updates/` on the board.

## 7. Tests and Checks

`camera-isp-sdk validate` catches, before the build, problems that would otherwise only show up on the board and
often without any error message:

* Image widths the capture DMA cannot take
* DCC files whose ID the ISP ignores
* An AE statistics grid outside the frame
* Tuning files made for another bit depth or Bayer order
* Link rates the D-PHY cannot carry

```bash Terminal theme={"system"}
task validate
```

```bash Output theme={"system"}
task: [validate] ./camera-isp-sdk --board t3-gem-o1 --build-dir build validate
imx219: ok
imx230: ok
imx477: ok
ov5647: ok
```

| Task | Description |
| - | - |
| `task test` | Unit tests of the Python tool and the GStreamer descriptor parser |
| `task check-overlays BASE_DTB=<dtb>` | Applies the overlays to the board's base DTB with `fdtoverlay` |
| `task gst:check-patch` | Checks that the plugin patch applies to TI's pinned commit |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.