|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +usage () { |
| 4 | + echo "USAGE: ./gen-vmlinux-headers.sh <arch> [<linux-repo-path> <kconfig-path>]" |
| 5 | + exit 1 |
| 6 | +} |
| 7 | + |
| 8 | +set -eu |
| 9 | + |
| 10 | +source $(dirname "$0")/helpers.sh |
| 11 | + |
| 12 | +WORKSPACE=$(pwd) |
| 13 | +BUILD_DIR="${BUILD_DIR:-$WORKSPACE/build}" |
| 14 | +GCC_VERSION=${GCC_VERSION:-"13"} |
| 15 | +OUTPUT_DIR="${OUTPUT_DIR:-$WORKSPACE/vmlinux.h}" |
| 16 | + |
| 17 | +TARGET_ARCH=$1 |
| 18 | +LINUX_REPO=${2:-$WORKSPACE/linux} |
| 19 | +KCONFIG=${3:-} |
| 20 | + |
| 21 | +if [ -z "${TARGET_ARCH}" ]; then |
| 22 | + echo "Error: Target architecture is not set" |
| 23 | + usage |
| 24 | +fi |
| 25 | + |
| 26 | +if [ ! -d "${LINUX_REPO}" ]; then |
| 27 | + echo "Error: Linux repo path is not found, LINUX_REPO=$LINUX_REPO" |
| 28 | + usage |
| 29 | +fi |
| 30 | + |
| 31 | +if [ ! -f "${KCONFIG}" ]; then |
| 32 | + KCONFIG=$(mktemp /tmp/kconfig.XXXX) |
| 33 | + cp $WORKSPACE/kconfigs/config.common $KCONFIG |
| 34 | + arch_conf="$WORKSPACE/kconfigs/config.${TARGET_ARCH}" |
| 35 | + if [ -f "$arch_conf" ]; then |
| 36 | + cat "$arch_conf" >> $KCONFIG |
| 37 | + fi |
| 38 | + echo "==== Using KCONFIG=$KCONFIG for kernel build" |
| 39 | + cat $KCONFIG |
| 40 | + echo "==== end of KCONFIG=$KCONFIG" |
| 41 | +fi |
| 42 | + |
| 43 | +mkdir -p "$BUILD_DIR" |
| 44 | +LINUX_REPO=$(realpath "$LINUX_REPO") |
| 45 | +KCONFIG=$(realpath "$KCONFIG") |
| 46 | + |
| 47 | +# Install the cross-compiler |
| 48 | +if [ "${TARGET_ARCH}" != "x86_64" ]; then |
| 49 | + compiler_id=$(arch_compiler_id "$TARGET_ARCH") |
| 50 | + sudo apt install -y \ |
| 51 | + "gcc-${GCC_VERSION}-${compiler_id}" \ |
| 52 | + "binutils-${compiler_id}" |
| 53 | + sudo update-alternatives --install \ |
| 54 | + /usr/bin/${compiler_id}-gcc \ |
| 55 | + ${compiler_id}-gcc \ |
| 56 | + /usr/bin/${compiler_id}-gcc-${GCC_VERSION} 100 |
| 57 | + sudo update-alternatives --set \ |
| 58 | + ${compiler_id}-gcc \ |
| 59 | + /usr/bin/${compiler_id}-gcc-${GCC_VERSION} |
| 60 | +fi |
| 61 | + |
| 62 | +build_arch(){ |
| 63 | + local arch="$1" |
| 64 | + local kbuild_output="$(realpath $BUILD_DIR/$arch)" |
| 65 | + mkdir -p "$kbuild_output" |
| 66 | + local arch_slug=$(arch_slug "$arch") |
| 67 | + local compiler_id=$(arch_compiler_id "$arch") |
| 68 | + |
| 69 | + echo "Building $arch ($arch_slug) into $kbuild_output..." |
| 70 | + ( |
| 71 | + cd "$LINUX_REPO" |
| 72 | + make O="$kbuild_output" \ |
| 73 | + ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \ |
| 74 | + tinyconfig |
| 75 | + "$LINUX_REPO/scripts/kconfig/merge_config.sh" -m \ |
| 76 | + -O "$kbuild_output" \ |
| 77 | + "$kbuild_output/.config" "${KCONFIG}" |
| 78 | + make O="$kbuild_output" \ |
| 79 | + ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \ |
| 80 | + olddefconfig |
| 81 | + |
| 82 | + make O="$kbuild_output" \ |
| 83 | + ARCH=$arch_slug CROSS_COMPILE="${compiler_id}-" \ |
| 84 | + -j$(nproc) all |
| 85 | + |
| 86 | + mkdir -p "$OUTPUT_DIR/$arch" |
| 87 | + bpftool btf dump file "$kbuild_output/vmlinux" format c \ |
| 88 | + > "$OUTPUT_DIR/$arch/vmlinux.h" |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +build_arch $TARGET_ARCH |
0 commit comments