# Compilation of kernel from sources

{% hint style="danger" %}
This section describes the compilation with JetPack 4.6. For compilation with JetPack 5.1 see: <https://github.com/airvolute/linux_jetson/tree/RC_JP_5_1_R35_2_1_airvolute>
{% endhint %}

## Creating a working directory

1. **Create working directory using Nvidia SDK manager.**
   * download and install NVIDIA SDK manager [official web](https://developer.nvidia.com/nvidia-sdk-manager)
   * open SDK manager
   * choose "Target Hardware" and "Linux" according to which system is image intended for. (Jetson Xavier NX modules / JetPack4.6 used in this example)
   * continue through all the steps (with jetson unconnected)
   * if SDK Manager show flash dialog, click on "Skip".
   * if installation is complete, click on "FINISH AND EXIT".
   * now working directory for target is created in '/nvidia/nvidia\_sdk/' folder. (JetPack\_4.6\_Linux\_JETSON\_XAVIER\_NX\_TARGETS in this example)
2. **Create working directory using cli command:**
   * assumed sdkmanager is installed
   * execute: `sdkmanager --cli install --logintype devzone --product Jetson --targetos Linux --version 4.6 --target JETSON_XAVIER_NX_TARGETS --deselect "Jetson SDK Components" --flash skip`

## Obtaining kernel sources

**Clone github repository into working directory:**

* open terminal in *`$target_working_directory/Linux_for_Tegra/`*
* clone git repository according to Jetson type:
  * xavier nx: `git clone` [`https://github.com/airvolute/kernel_sources_xaviernx.git`](https://github.com/airvolute/kernel_sources_xaviernx.git)\`\`
  * orin nx: TBD
* `cd` to cloned repo
* checkout branch according to JetPack version of working directory:

  ```
  git checkout <branch>
  ```

**Branches matrix:**

| Jetson type | JetPack version | \<branch>                    |
| ----------- | --------------- | ---------------------------- |
| xavier nx   | J.P. 4.6.0      | JP\_46\_L4T\_3261\_airvolute |
|             |                 |                              |

## Compilation

Assumed that working directory and kernel sources obtained successfully.

1. **Setup variables**

   ```
   NVIDIA_DEVELOPMENT_FOLDER=JetPack_4.6_Linux_JETSON_XAVIER_NX_TARGETS
   TEGRA_KERNEL_OUT=$HOME/Downloads/kernel_out
   JETPACK_DIR=$HOME/nvidia/nvidia_sdk/$NVIDIA_DEVELOPMENT_FOLDER/Linux_for_Tegra
   ```
2. **Download other dependencies**

   ```
   sudo apt update
   sudo apt install build-essential bc
   ```
3. **Download and setup toolchain**

   ```
   cd ~
   mkdir -p $HOME/l4t-gcc
   cd $HOME/l4t-gcc

   # Reuse existing download, if any (copy this as a block starting with if and ending with fi)
   if ! test -e  gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.gz; then 
   wget -O  gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.gz http://releases.linaro.org/components/toolchain/binaries/7.3-2018.05/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
   tar -xf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.gz
   fi

   cd ~
   ```
4. **Setup compilation of kernel sources**

   ```
   # Environment variables
   KERNEL_DIR=$HOME/nvidia/nvidia_sdk/$NVIDIA_DEVELOPMENT_FOLDER/Linux_for_Tegra/kernel_sources_xaviernx/kernel/kernel-4.9
   CROSS_COMPILE_AARCH64=$HOME/l4t-gcc/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-


   # Create folder for kernel output
   rm -r $TEGRA_KERNEL_OUT 
   mkdir -p $TEGRA_KERNEL_OUT 

   # Start with compilation
   cd $KERNEL_DIR
   make mrproper
   make ARCH=arm64 LOCALVERSION="-tegra" CROSS_COMPILE="${CROSS_COMPILE_AARCH64}" O=$TEGRA_KERNEL_OUT tegra_defconfig
   make ARCH=arm64 LOCALVERSION="-tegra" CROSS_COMPILE="${CROSS_COMPILE_AARCH64}" O=$TEGRA_KERNEL_OUT menuconfig

   # update menuconfig now if needed else exit GUI
   ```
5. **Compilation of sources**

   Option A : compile kernel

   ```
   cd $KERNEL_DIR
   make ARCH=arm64 LOCALVERSION="-tegra" CROSS_COMPILE="${CROSS_COMPILE_AARCH64}" O=$TEGRA_KERNEL_OUT -j$(nproc) --output-sync=target Image

   # Copy files
   rm $JETPACK_DIR/kernel/Image
   sudo rm $JETPACK_DIR/rootfs/boot/Image

   cp $TEGRA_KERNEL_OUT/arch/arm64/boot/Image $JETPACK_DIR/kernel
   sudo cp $TEGRA_KERNEL_OUT/arch/arm64/boot/Image $JETPACK_DIR/rootfs/boot
   ```

   Option B : compile device tree

   ```
   cd $KERNEL_DIR
   make ARCH=arm64 LOCALVERSION="-tegra" CROSS_COMPILE="${CROSS_COMPILE_AARCH64}" O=$TEGRA_KERNEL_OUT -j$(nproc) --output-sync=target dtbs

   # Copy files
   rm -r $JETPACK_DIR/kernel/dtb/
   sudo rm  $JETPACK_DIR/rootfs/boot/tegra194-p3668-all-p3509-0000.dtb

   cp -a $TEGRA_KERNEL_OUT/arch/arm64/boot/dts/. $JETPACK_DIR/kernel/dtb/
   cp -a $TEGRA_KERNEL_OUT/arch/arm64/boot/dts/tegra194-p3668-all-p3509-0000.dtb $JETPACK_DIR/kernel/dtb/tegra194-p3668-all-p3509-0000.dtb
   sudo cp -a $TEGRA_KERNEL_OUT/arch/arm64/boot/dts/tegra194-p3668-all-p3509-0000.dtb $JETPACK_DIR/rootfs/boot/tegra194-p3668-all-p3509-0000.dtb
   ```

   Option C : compile modules

   ```
   cd $KERNEL_DIR
   make ARCH=arm64 LOCALVERSION="-tegra" CROSS_COMPILE="${CROSS_COMPILE_AARCH64}" O=$TEGRA_KERNEL_OUT -j$(nproc) --output-sync=target modules

   # Install modules
   make ARCH=arm64 O=$TEGRA_KERNEL_OUT modules_install INSTALL_MOD_STRIP=1 CROSS_COMPILE=$CROSS_COMPILE_AARCH64 INSTALL_MOD_PATH=$JETPACK_DIR/images

   cd $JETPACK_DIR/images
   tar --owner root --group root -cjf kernel_supplements.tbz2 lib/modules
   mv kernel_supplements.tbz2 $JETPACK_DIR/kernel/
   ```
6. **Applying compiled files to working directory**

   ```
   # Apply changes 
   cd $JETPACK_DIR
   sudo ./apply_binaries.sh -t False
   ```
7. **Deploying compiled outputs to runnning Jetson target**

Connect Jetson target to host PC through configuration micro usb and execute following commands to deploy compilation outputs to running target.

* **Kernel image deployment**

  ```
  scp $JETPACK_DIR/kernel/Image dcs_user@192.168.55.1:/boot/
  ```
* **Device tree deployment**

  ```
  scp $JETPACK_DIR/kernel/dtb/tegra194-p3668-all-p3509-0000.dtb dcs_user@192.168.55.1:/boot/tegra194-p3668-all-p3509-0000.dtb
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.airvolute.com/autopilots/common-features/nvidia-jetson-kernel-customization/compilation-of-kernel-from-sources.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
