Compiling the Kernel (NVIDIA DRIVE Linux)

This topic contains instructions for compiling the Linux Kernel source in your Linux SDK product.
Prerequisites
You have unpacked the Kernel Source Code:
bash drive-t186ref-linux-*-oss-src.run
bash drive-t186ref-foundation-*-toolchain.run
This script creates this directory:
drive-oss-src
The top-level directory layout has this appearance:
hardware
toolchains
drive-oss-src
drive-t186ref-foundation
drive-t186ref-foundation_src
drive-t186ref-linux
drive-t186ref-linux_src
To compile the kernel
1. Set up the environment macros: The LOCAL_VERSION environment variable appends -tegra to the kernel version and modules.
export ARCH=arm64
export CROSS_COMPILE=<top>/toolchains/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
export LOCALVERSION="-tegra"
2. Set the kernel source directory as the current working directory:
cd <top>/drive-oss-src
3. Enter the following command to apply the rt-patches.
bash kernel/scripts/rt-patch.sh apply-patches
4. Create an output directory, and then configure the kernel for the board:
mkdir out-t186ref-linux
make -C kernel O=${PWD}/out-t186ref-linux clean
5. Add the following command:
make -C kernel O=${PWD}/out-t186ref-linux tegra_defconfig
6. Build the kernel:
make -j3 -C kernel O=${PWD}/out-t186ref-linux
 
Note:
If the above command fails, enter the make command without the j<number> option.
7. To flash the built images, copy the compressed (zImage) and uncompressed (Image) kernel images to the kernel top directory with the following commands:
cp ${PWD}/out-t186ref-linux/arch/arm64/boot/zImage <top>/drive-t186ref-linux/kernel-rt_patches
cp ${PWD}/out-t186ref-linux/arch/arm64/boot/Image <top>/drive-t186ref-linux/kernel-rt_patches
Caution: Before copying the new kernel images, create a backup of the default kernels provided.
8. Consolidate the built kernel modules in the build directory with the following commands:
export INSTALL_MOD_PATH=${PWD}/out-t186ref-linux
make -C kernel O=${PWD}/out-t186ref-linux modules_install
9. (Applies to: Ubuntu targetfs) Copy the built modules to the root filesystem path with the following commands:
sudo cp -a ${PWD}/out-t186ref-linux/lib/modules/* <top>/drive-t186ref-linux/targetfs/lib/modules
 
Note:
To ensure that all modules are loaded, at the target side, execute the commands:
depmod-a
sync
The sync command must be executed as superuser. From the next boot onward, the kernel modules are auto-loaded on boot.
10. If there is a change in the device tree source files, copy the newly generated DTB files:
cp <top>/drive-oss-src/out-t186ref-linux/arch/arm64/boot/dts/<dtb_file> <top>/drive-t186ref-linux/kernel