Skip to content

Commit 2371b84

Browse files
authored
Merge pull request #29 from RinHizakura/main
Support to build lkl from source in Makefile
2 parents 47157b2 + f08907f commit 2371b84

4 files changed

Lines changed: 161 additions & 65 deletions

File tree

.github/workflows/build-lkl.yml

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -97,77 +97,20 @@ jobs:
9797
sudo apt-get update
9898
sudo apt-get install -y build-essential flex bison bc libelf-dev
9999
100-
- name: Shallow-fetch LKL at pinned commit
101-
run: |
102-
git init lkl && cd lkl
103-
git remote add origin "https://github.com/${{ env.LKL_UPSTREAM }}.git"
104-
git fetch --depth=1 origin ${{ needs.check-upstream.outputs.lkl_commit }}
105-
git checkout FETCH_HEAD
106-
107-
- name: Configure LKL
108-
working-directory: lkl
109-
run: |
110-
make ARCH=lkl defconfig
111-
./scripts/config --enable CONFIG_DEVTMPFS
112-
./scripts/config --enable CONFIG_DEVTMPFS_MOUNT
113-
./scripts/config --enable CONFIG_DEVPTS_FS
114-
./scripts/config --enable CONFIG_DEBUG_INFO
115-
./scripts/config --set-val CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT y
116-
./scripts/config --enable CONFIG_GDB_SCRIPTS
117-
./scripts/config --enable CONFIG_SCHED_DEBUG
118-
./scripts/config --enable CONFIG_PROC_SYSCTL
119-
./scripts/config --enable CONFIG_PRINTK
120-
./scripts/config --enable CONFIG_TRACEPOINTS
121-
./scripts/config --enable CONFIG_FTRACE
122-
./scripts/config --enable CONFIG_DEBUG_FS
123-
./scripts/config --disable CONFIG_MODULES
124-
./scripts/config --disable CONFIG_SOUND
125-
./scripts/config --disable CONFIG_USB_SUPPORT
126-
./scripts/config --disable CONFIG_INPUT
127-
./scripts/config --disable CONFIG_NFS_FS
128-
./scripts/config --disable CONFIG_CIFS
129-
make ARCH=lkl olddefconfig
130-
131-
- name: Build LKL kernel
132-
working-directory: lkl
133-
run: make ARCH=lkl -j$(nproc)
134-
135-
- name: Build liblkl.a
136-
working-directory: lkl
137-
run: make -C tools/lkl -j$(nproc)
138-
139-
- name: Verify symbols
140-
working-directory: lkl
141-
run: |
142-
test -f tools/lkl/liblkl.a || { echo "liblkl.a not found"; exit 1; }
143-
for sym in lkl_init lkl_start_kernel lkl_cleanup lkl_syscall \
144-
lkl_strerror lkl_disk_add lkl_mount_dev \
145-
lkl_host_ops lkl_dev_blk_ops; do
146-
if ! nm tools/lkl/liblkl.a 2>/dev/null | awk -v s="$sym" \
147-
'$3 == s && $2 ~ /^[TtDdBbRr]$/ {found=1} END {exit !found}'; then
148-
echo "MISSING symbol: ${sym}"; exit 1
149-
fi
150-
done
100+
- name: Build LKL from source
101+
env:
102+
LKL_REF: ${{ needs.check-upstream.outputs.lkl_commit }}
103+
run: make build-lkl
151104

152105
- name: Package
153-
working-directory: lkl
154106
run: |
155-
mkdir -p pkg
156-
cp tools/lkl/liblkl.a pkg/
157-
cp tools/lkl/include/lkl.h pkg/ 2>/dev/null || true
158-
cp tools/lkl/include/lkl/autoconf.h pkg/ 2>/dev/null || true
159-
cp scripts/gdb/vmlinux-gdb.py pkg/ 2>/dev/null || true
160-
echo "commit=$(git rev-parse HEAD)" > pkg/BUILD_INFO
161-
echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> pkg/BUILD_INFO
162-
echo "arch=${{ matrix.arch }}" >> pkg/BUILD_INFO
163-
cd pkg && sha256sum ./* > sha256sums.txt
164-
cd .. && tar czf liblkl-${{ matrix.arch }}.tar.gz -C pkg .
107+
tar czf liblkl-${{ matrix.arch }}.tar.gz lkl-${{ matrix.arch }}/
165108
166109
- name: Upload artifact
167110
uses: actions/upload-artifact@v7
168111
with:
169112
name: lkl-${{ matrix.arch }}
170-
path: lkl/liblkl-${{ matrix.arch }}.tar.gz
113+
path: liblkl-${{ matrix.arch }}.tar.gz
171114
retention-days: 3
172115

173116
# ---- Publish: replace lkl-nightly release with latest artifacts ----

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ KCONFIG_CONF := configs/Kconfig
1515

1616
# Targets that don't require .config
1717
CONFIG_TARGETS := config defconfig oldconfig savedefconfig clean distclean indent \
18-
check-unit fetch-lkl fetch-minislirp install-hooks \
18+
check-unit fetch-lkl build-lkl fetch-minislirp install-hooks \
1919
guest-bins stress-bins rootfs
2020
CONFIG_GENERATORS := config defconfig oldconfig
2121

mk/deps.mk

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
# External dependency fetching (LKL, minislirp)
1+
# External dependency fetching / building (LKL, minislirp)
2+
3+
# FORCE_LKL_BUILD=1 – clone lkl/linux and build liblkl.a from source.
4+
# The source tree is kept in build/lkl-src so
5+
# subsequent builds reuse it (pass LKL_DIR= to override).
6+
# (default) – download prebuilt tarball from the lkl-nightly release.
7+
8+
ifeq ($(FORCE_LKL_BUILD),1)
9+
10+
$(LKL_LIB):
11+
@echo " BUILD lkl (from source)"
12+
$(Q)./scripts/build-lkl.sh $(ARCH)
13+
14+
else
215

316
# Auto-fetch LKL if missing
417
$(LKL_LIB):
518
@echo " FETCH lkl"
619
$(Q)./scripts/fetch-lkl.sh $(ARCH)
720

21+
endif
22+
823
.PHONY: fetch-lkl
924
fetch-lkl:
1025
@echo " FETCH lkl"
1126
$(Q)./scripts/fetch-lkl.sh $(ARCH)
1227

28+
.PHONY: build-lkl
29+
build-lkl:
30+
@echo " BUILD lkl (from source)"
31+
$(Q)./scripts/build-lkl.sh $(ARCH)
32+
1333
# Auto-fetch minislirp if missing (shallow clone, no submodule).
1434
# $(wildcard) evaluates at parse time, so if minislirp has not been fetched yet
1535
# SLIRP_SRCS is empty. Guard: fetch and re-eval so the wildcard picks up

scripts/build-lkl.sh

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: MIT
3+
# Build liblkl.a from source (lkl/linux) and populate lkl-<ARCH>/.
4+
#
5+
# Usage: ./scripts/build-lkl.sh [ARCH]
6+
# ARCH defaults to the host architecture (x86_64 or aarch64).
7+
# Override LKL_DIR to change the output directory.
8+
# Override LKL_SRC to reuse an existing source tree (skip clone).
9+
# Override LKL_REF to check out a specific branch/tag/commit
10+
# (default: HEAD of master).
11+
12+
set -eu
13+
14+
case "${1:-$(uname -m)}" in
15+
x86_64 | amd64) ARCH="x86_64" ;;
16+
aarch64 | arm64) ARCH="aarch64" ;;
17+
*)
18+
echo "error: unsupported architecture: ${1:-$(uname -m)}" >&2
19+
exit 1
20+
;;
21+
esac
22+
23+
LKL_DIR="${LKL_DIR:-lkl-${ARCH}}"
24+
LKL_SRC="${LKL_SRC:-build/lkl-src}"
25+
LKL_UPSTREAM="https://github.com/lkl/linux"
26+
27+
die() { echo "error: $*" >&2; exit 1; }
28+
29+
# ---- Clone or update source tree ----------------------------------------
30+
31+
if [ -d "${LKL_SRC}/.git" ]; then
32+
echo " SRC ${LKL_SRC} (exists, skipping clone)"
33+
if [ -n "${LKL_REF:-}" ]; then
34+
echo " CHECKOUT ${LKL_REF}"
35+
git -C "${LKL_SRC}" fetch --depth=1 origin "${LKL_REF}"
36+
git -C "${LKL_SRC}" checkout FETCH_HEAD
37+
fi
38+
else
39+
echo " CLONE ${LKL_UPSTREAM} -> ${LKL_SRC}"
40+
mkdir -p "$(dirname "${LKL_SRC}")"
41+
42+
if [ -n "${LKL_REF:-}" ]; then
43+
# Shallow clone of a specific ref.
44+
git clone --depth=1 --branch "${LKL_REF}" \
45+
"${LKL_UPSTREAM}" "${LKL_SRC}"
46+
else
47+
# Shallow clone of default branch.
48+
git clone --depth=1 "${LKL_UPSTREAM}" "${LKL_SRC}"
49+
fi
50+
fi
51+
52+
# ---- Configure -----------------------------------------------------------
53+
54+
echo " CONFIG ARCH=lkl defconfig"
55+
make -C "${LKL_SRC}" ARCH=lkl defconfig
56+
57+
# Enable features required by kbox (mirrors build-lkl.yml).
58+
for opt in \
59+
CONFIG_DEVTMPFS \
60+
CONFIG_DEVTMPFS_MOUNT \
61+
CONFIG_DEVPTS_FS \
62+
CONFIG_DEBUG_INFO \
63+
CONFIG_GDB_SCRIPTS \
64+
CONFIG_SCHED_DEBUG \
65+
CONFIG_PROC_SYSCTL \
66+
CONFIG_PRINTK \
67+
CONFIG_TRACEPOINTS \
68+
CONFIG_FTRACE \
69+
CONFIG_DEBUG_FS \
70+
; do
71+
"${LKL_SRC}/scripts/config" --file "${LKL_SRC}/.config" --enable "${opt}"
72+
done
73+
74+
"${LKL_SRC}/scripts/config" --file "${LKL_SRC}/.config" \
75+
--set-val CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT y
76+
77+
for opt in \
78+
CONFIG_MODULES \
79+
CONFIG_SOUND \
80+
CONFIG_USB_SUPPORT \
81+
CONFIG_INPUT \
82+
CONFIG_NFS_FS \
83+
CONFIG_CIFS \
84+
; do
85+
"${LKL_SRC}/scripts/config" --file "${LKL_SRC}/.config" --disable "${opt}"
86+
done
87+
88+
make -C "${LKL_SRC}" ARCH=lkl olddefconfig
89+
90+
# ---- Build ---------------------------------------------------------------
91+
92+
NPROC=$(nproc 2>/dev/null || echo 1)
93+
94+
echo " BUILD ARCH=lkl kernel (-j${NPROC})"
95+
make -C "${LKL_SRC}" ARCH=lkl -j"${NPROC}"
96+
97+
echo " BUILD tools/lkl (-j${NPROC})"
98+
make -C "${LKL_SRC}/tools/lkl" -j"${NPROC}"
99+
100+
# ---- Verify --------------------------------------------------------------
101+
102+
test -f "${LKL_SRC}/tools/lkl/liblkl.a" \
103+
|| die "build succeeded but liblkl.a not found"
104+
105+
echo " VERIFY symbols"
106+
for sym in lkl_init lkl_start_kernel lkl_cleanup lkl_syscall \
107+
lkl_strerror lkl_disk_add lkl_mount_dev \
108+
lkl_host_ops lkl_dev_blk_ops; do
109+
if ! nm "${LKL_SRC}/tools/lkl/liblkl.a" 2>/dev/null \
110+
| awk -v s="$sym" '$3==s && $2~/^[TtDdBbRr]$/{found=1} END{exit !found}'; then
111+
die "MISSING symbol: ${sym}"
112+
fi
113+
done
114+
115+
# ---- Install into lkl-<ARCH>/ -------------------------------------------
116+
117+
echo " INSTALL ${LKL_DIR}/"
118+
mkdir -p "${LKL_DIR}"
119+
120+
cp "${LKL_SRC}/tools/lkl/liblkl.a" "${LKL_DIR}/"
121+
cp "${LKL_SRC}/tools/lkl/include/lkl.h" "${LKL_DIR}/" 2>/dev/null || true
122+
cp "${LKL_SRC}/tools/lkl/include/lkl/autoconf.h" "${LKL_DIR}/" 2>/dev/null || true
123+
cp "${LKL_SRC}/scripts/gdb/vmlinux-gdb.py" "${LKL_DIR}/" 2>/dev/null || true
124+
125+
printf 'commit=%s\ndate=%s\narch=%s\n' \
126+
"$(git -C "${LKL_SRC}" rev-parse HEAD)" \
127+
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
128+
"${ARCH}" \
129+
> "${LKL_DIR}/BUILD_INFO"
130+
131+
( cd "${LKL_DIR}" && sha256sum ./* > sha256sums.txt )
132+
133+
echo "OK: ${LKL_DIR}/liblkl.a"

0 commit comments

Comments
 (0)