Skip to content

Commit e4019ad

Browse files
authored
Merge pull request #92 from sixtyfourktec/linux
Linux
2 parents f5207a8 + 0a82b89 commit e4019ad

9 files changed

Lines changed: 313 additions & 89 deletions

File tree

aliases/virtual/kernel/linux.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
multiPackage:
2+
image: ${CONFIG_SELECT_LINUX:-kernel::linux}-image
3+
modules: ${CONFIG_SELECT_LINUX:-kernel::linux}-modules
4+
dtbs: ${CONFIG_SELECT_LINUX:-kernel::linux}-dtbs
5+
kmod-dist: ${CONFIG_SELECT_LINUX:-kernel::linux}-kmod-dist
6+
headers: ${CONFIG_SELECT_LINUX:-kernel::linux}-headers

classes/linux.yaml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Convenience methods for creating a Linux kernel
2+
#
3+
# Here we define helper methods for building the linux kernel and everything
4+
# around it. This means the kernel modules, any dtb's and dev targets like the
5+
# headers or an kmod-dist for building out-of-tree kernel modules.
6+
#
7+
# In its simplest form the usage looks something like the following. Note, that
8+
# the kernel is downloaded into the "linux" directory and that the
9+
# configuration files are put into the "conf" directory. Obviously you are free
10+
# to arrange this somehow differently. Of course you only need to implement the
11+
# targets required by your other recipes:
12+
#
13+
# inherit: [linux]
14+
#
15+
# ...
16+
#
17+
# metaEnvironment:
18+
# PKG_VERSION: 6.1.114
19+
#
20+
# checkoutSCM:
21+
# - scm: url
22+
# url: ${KERNEL_MIRROR}/linux/kernel/v6.x/linux-${PKG_VERSION}.tar.xz
23+
# digestSHA1: 72f98a7aa831dba4d56b6c28b1a5333b04fbae35
24+
# stripComponents: 1
25+
# dir: linux
26+
# - scm: import
27+
# url: src/linux
28+
# dir: conf
29+
#
30+
# buildScript: |
31+
# linuxConfig $1/linux $1/conf/linux.config
32+
#
33+
# multiPackage:
34+
# "":
35+
# buildScript: linuxBuild
36+
#
37+
# multiPackage:
38+
# image:
39+
# inherit: [linux::image]
40+
# packageScript: linuxInstallImage $1
41+
#
42+
# modules:
43+
# inherit: [linux::modules]
44+
# packageScript: linuxInstallModules $1
45+
#
46+
# kmod-dist:
47+
# inherit: [linux::kmod-dist]
48+
# packageScript: linuxInstallKmodDist $1
49+
#
50+
# dtbs:
51+
# inherit: [linux::dtbs]
52+
# buildScript: linuxBuildDtbs
53+
# packageScript: linuxInstallDtbs $1
54+
#
55+
# headers:
56+
# inherit: [linux::headers]
57+
# buildScript: linuxBuildHeaders
58+
# packageScript: linuxInstallHeaders $1
59+
#
60+
inherit: [cpackage, make, pkg-config]
61+
62+
depends:
63+
- tools:
64+
target-toolchain: host-compat-toolchain
65+
depends:
66+
- libs::elfutils-libelf-dev
67+
- libs::openssl-dev
68+
- name: kernel::kmod
69+
use: [tools]
70+
71+
privateEnvironment:
72+
LINUX_ARCH: "$(if-then-else,$(or,$(eq,${ARCH},i386),$(eq,${ARCH},x86_64)),x86,$(if-then-else,\
73+
$(or,$(eq,${ARCH},sh),$(eq,${ARCH},sh64)),sh,$(if-then-else,$(or,$(eq,${ARCH},sparc32),\
74+
$(eq,${ARCH},sparc64)),sparc,$ARCH)))"
75+
76+
checkoutSCM:
77+
# Always download a script for extracting the files required to build
78+
# external kernel modules. If the kernel itself doesn't have one, we will
79+
# use this one.
80+
scm: url
81+
url: https://raw.githubusercontent.com/torvalds/linux/refs/tags/v6.11/scripts/package/install-extmod-build
82+
digestSHA1: 47eb760cf215cb3a15752186a18715fb04d9ab95
83+
dir: kmod-script
84+
85+
buildVars: [LINUX_ARCH, ARCH, CROSS_COMPILE, CC]
86+
buildTools: [m4, bison, flex, target-toolchain, host-toolchain]
87+
buildSetup: |
88+
# make sure we find our kmod-dist script in the package step
89+
ln -sf $1/kmod-script .
90+
91+
# Build the initial linux config. This will be used by all other steps.
92+
#
93+
# $1: linux kernel source directory
94+
# $2: defconfig (optional)
95+
# - if this is an actual file, use it as the config
96+
# - if this is not a file, check if a defconfig for the target arch by
97+
# that name exists and if yes, use that one
98+
linuxConfig()
99+
{
100+
DEF_CFG=${2:-$1/arch/${LINUX_ARCH}/configs/defconfig}
101+
if [[ -f "$1/arch/${LINUX_ARCH}/configs/${DEF_CFG}" ]] ; then
102+
DEF_CFG=$1/arch/${LINUX_ARCH}/configs/${DEF_CFG}
103+
fi
104+
# check if the defconfig file exists
105+
if [[ ! -f "$DEF_CFG" ]]; then
106+
>&2 echo "Don't know how to use '$DEF_CFG' as linux kernel config!"
107+
false
108+
fi
109+
# check if the source file is newer than .config
110+
if [[ -f .config && .config -nt $DEF_CFG ]]; then
111+
return 0
112+
fi
113+
# redo the .config file
114+
cp -u "$DEF_CFG" .config
115+
makeSequential -C $1 O=$PWD \
116+
olddefconfig
117+
}
118+
119+
# Build the actual target linux image and all configured modules
120+
linuxBuild()
121+
{
122+
makeParallel \
123+
CC="$CC" \
124+
$(basename $(make -s image_name)) modules
125+
}

classes/linux/dtbs.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
inherit: [linux]
2+
3+
buildTools: [target-toolchain]
4+
buildVars: [CROSS_COMPILE]
5+
buildSetup: |
6+
# Build the device tree files.
7+
#
8+
# $1: device tree file(s) (optional)
9+
# - if this isn't provided, all default dtb's in the kernel directory
10+
# are build
11+
linuxBuildDtbs()
12+
{
13+
makeParallel ${1:-dtbs} "${@:2}"
14+
}
15+
16+
packageSetup: |
17+
# Install the device tree files.
18+
#
19+
# $1: linux kernel build directory
20+
# $2: device tree file(s) (optional)
21+
# - if this isn't provided, all default dtb's in the kernel directory
22+
# are installed
23+
linuxInstallDtbs()
24+
{
25+
if [[ -n "${2:-}" ]]; then
26+
if [[ -d "$1/arch/$LINUX_ARCH/boot/dts" ]] ; then
27+
make -C "$1" INSTALL_DTBS_PATH="$PWD" dtbs_install
28+
fi
29+
else
30+
for i in "${@:2}" ; do
31+
cp "$1/arch/${LINUX_ARCH}/boot/dts/$i" "$PWD"
32+
done
33+
fi
34+
}

classes/linux/headers.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
inherit: [linux]
2+
3+
buildSetup: |
4+
# Build the linux kernel headers.
5+
linuxBuildHeaders()
6+
{
7+
make headers_install
8+
}
9+
10+
packageSetup: |
11+
# Install the linux kernel headers.
12+
#
13+
# $1: linux kernel build directory
14+
linuxInstallHeaders()
15+
{
16+
cp -a $1/usr .
17+
}

classes/linux/image.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
inherit: [linux]
2+
3+
packageVars: [BASEMENT_DEBUG]
4+
packageSetup: |
5+
# Install the linux kernel image, the system.map and the .config used to
6+
# build the kernel.
7+
#
8+
# $1: linux kernel build directory
9+
# $2: destination image name (optional)
10+
linuxInstallImage()
11+
{
12+
src_img_name=$(make -s -C "$1" image_name)
13+
dst_img_name=${2:-$(basename $src_img_name)}
14+
cp "$1/$src_img_name" "${dst_img_name}"
15+
cp "$1/System.map" "${dst_img_name}-System.map"
16+
cp "$1/.config" "${dst_img_name}-config"
17+
18+
if [[ ${BASEMENT_DEBUG:-0} != "0" ]] ; then
19+
mkdir -p .debug
20+
cp "$1/$src_img_name" ".debug/${dst_img_name}"
21+
fi
22+
}

classes/linux/kmod-dist.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
inherit: [linux]
2+
3+
packageSetup: |
4+
_SCRIPT_DIR=$1/kmod-script
5+
6+
# Install the linux kernel modules distribution for building out-of-tree
7+
# kernel modules.
8+
#
9+
# $1: linux kernel build directory
10+
linuxInstallKmodDist()
11+
{
12+
OUT=$PWD
13+
# If the kernel itself provides a script for extracting the files required to
14+
# build external kernel modules, use that, otherwise use the downloaded fixed
15+
# version.
16+
# Default is kernel built-in script:
17+
SCRIPT=$1/source/scripts/package/install-extmod-build
18+
if [[ ! -e $SCRIPT ]]; then
19+
SCRIPT=$_SCRIPT_DIR/install-extmod-build
20+
fi
21+
pushd $1
22+
MAKE=make HOSTCC=gcc SRCARCH=$LINUX_ARCH srctree=$1/source KCONFIG_CONFIG=$1/.config \
23+
sh "$SCRIPT" "$OUT"
24+
popd
25+
}

classes/linux/menuconfig.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
inherit: [linux]
2+
3+
depends:
4+
- tools:
5+
target-toolchain: host-compat-toolchain
6+
depends:
7+
- libs::ncurses-dev
8+
9+
buildSetup: |
10+
# Calls the linux savedefconfig target
11+
#
12+
# Especially in the sandbox this can be tricky. Provide some convenience method.
13+
linuxSavedefconfig()
14+
{
15+
makeParallel savedefconfig
16+
}
17+
18+
# Calls the linux menuconfig target
19+
#
20+
# Especially in the sandbox this can be tricky. Provide some convenience method.
21+
linuxMenuconfig()
22+
{
23+
NCDEP=${BOB_DEP_PATHS[libs::ncurses-dev]}
24+
makeParallel \
25+
HOSTCC="gcc" \
26+
HOSTCFLAGS="-I${NCDEP}/usr/include -DCURSES_LOC=\"<ncurses.h>\" -DLOCALE" \
27+
HOSTLDLIBS="-L$(gcc --print-sysroot)/usr/lib -L${NCDEP}/usr/lib -lncurses -ltinfo" \
28+
menuconfig
29+
# Always save a defconfig
30+
linuxSavedefconfig
31+
}

classes/linux/modules.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
inherit: [linux]
2+
3+
packageToolsWeak: [kmod]
4+
packageVars: [BASEMENT_DEBUG, OBJCOPY]
5+
packageSetup: |
6+
# Install the built-in linux kernel modules.
7+
#
8+
# $1: linux kernel build directory
9+
linuxInstallModules()
10+
{
11+
make -C "$1" \
12+
INSTALL_MOD_PATH="$PWD" modules_install
13+
rm lib/modules/*/build
14+
15+
# Strip all modules. Shamelessly ripped from builddeb...
16+
for module in $(find lib/modules/ -name *.ko -printf '%P\n'); do
17+
module=lib/modules/$module
18+
mkdir -p $(dirname lib/debug/$module)
19+
# only keep debug symbols in the debug file
20+
if [[ ${BASEMENT_DEBUG:-0} != "0" ]] ; then
21+
$OBJCOPY --only-keep-debug $module lib/debug/$module
22+
fi
23+
# strip original module from debug symbols
24+
$OBJCOPY --strip-debug $module
25+
# then add a link to those
26+
if [[ ${BASEMENT_DEBUG:-0} != "0" ]] ; then
27+
$OBJCOPY --add-gnu-debuglink=lib/debug/$module $module
28+
fi
29+
done
30+
}

0 commit comments

Comments
 (0)