Skip to content

Commit e2f621f

Browse files
committed
Enable building a toolchain for Linux
Leverage coreboot's buildgcc to build a suitable Linux toolchain if needed. Signed-off-by: David Hendricks <david.hendricks@gmail.com>
1 parent abc61a4 commit e2f621f

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

Makefile.inc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ endif
120120
# example "x86_64-linux-gnu-gcc-10".
121121
ifeq ($(CROSS_COMPILING), 1)
122122
INITRAMFS_BUILD_GOROOT ?= /usr/local/go
123+
ifeq ($(shell which $(LINUX_GCC_TUPLE)gcc),)
124+
BUILD_LINUX_WITH_COREBOOT_TOOLCHAIN = 1
125+
CROSS_COMPILE ?= $(COREBOOT_BUILD_DIR)/util/crossgcc/xgcc/bin/$(LINUX_GCC_TUPLE)
126+
else
127+
BUILD_LINUX_WITH_COREBOOT_TOOLCHAIN = 0
128+
CROSS_COMPILE ?= $(LINUX_GCC_TUPLE)
129+
endif
123130
else
124131
# FIXME: Also need to fix behavior when we're not cross-compilng, but system's
125132
# go version doesn't match the version we're using to build the initramfs...
@@ -201,8 +208,8 @@ $(COREBOOT_TOOLCHAIN_OUT)/bin/iasl: $(COREBOOT_DEPS_FLAG) $(COREBOOT_PATCH_FLAG)
201208
tar xf $(COREBOOT_TOOLCHAIN_CACHE) -C $(dir $(COREBOOT_TOOLCHAIN_OUT)); \
202209
touch $@; \
203210
else \
204-
COREBOOT_BUILD_DIR=$(COREBOOT_BUILD_DIR) CPUS=$(NPROC) MAKE=$(MAKE) ARCH=$(ARCH) TOOLS_DIR=$(TOOLS_DIR) \
205-
$(TOOLS_DIR)/build_toolchain.sh; \
211+
COREBOOT_BUILD_DIR=$(COREBOOT_BUILD_DIR) CPUS=$(NPROC) MAKE=$(MAKE) ARCH=$(COREBOOT_TOOLCHAIN_ARCH) \
212+
TOOLS_DIR=$(TOOLS_DIR) $(TOOLS_DIR)/build_toolchain.sh coreboot; \
206213
fi
207214

208215
# Creates coreboot config file using the user-provided one and feeding it to make olddefconfig.
@@ -236,19 +243,28 @@ wipe-coreboot:
236243
# Top-level initramfs target, for use by humans.
237244
kernel: $(KERNEL_OUT)
238245

246+
kernel_toolchain: $(COREBOOT_DEPS_FLAG) $(COREBOOT_PATCH_FLAG)
247+
ifeq "$(BUILD_LINUX_WITH_COREBOOT_TOOLCHAIN)" "1"
248+
echo 'Building cross toolchain for Linux'
249+
COREBOOT_BUILD_DIR=$(COREBOOT_BUILD_DIR) CPUS=$(NPROC) MAKE=$(MAKE) ARCH=$(LINUX_ARCH) \
250+
TOOLS_DIR=$(TOOLS_DIR) $(TOOLS_DIR)/build_toolchain.sh linux;
251+
else
252+
echo 'Building Linux with toolchain tuple: $(CROSS_COMPILE)'
253+
endif
254+
239255
# Kernel build target. Requires initrmafs image, config and patches.
240256
ifneq "$(INITRAMFS_IN)" ""
241-
$(KERNEL_OUT): $(INITRAMFS_IN) $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG)
257+
$(KERNEL_OUT): $(INITRAMFS_IN) $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG) kernel_toolchain
242258
else
243-
$(KERNEL_OUT): $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG)
259+
$(KERNEL_OUT): $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG) $(KERNEL_TOOLCHAIN) $(KERNEL_TOOLCHAIN)
244260
endif
245261
@# If lzma(1) is not available, kernel build will fall back to gzip and we don't want that.
246262
@if ! lzma -h > /dev/null 2>/dev/null; then \
247263
echo ' *** Please install the lzma CLI utility.' >&2; \
248264
echo ' *** In RedHat distros it`s provided by xz-lzma-compat, in Debian/Ubuntu it`s provided by xz-utils.' >&2; \
249265
exit 1; \
250266
fi
251-
MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) -j$(NPROC) KCFLAGS=-pipe
267+
ARCH=$(LINUX_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) -j$(NPROC) KCFLAGS=-pipe
252268
[ "$@" = "$(KERNEL_OUT_DEFAULT)" ] || cp -v "$(KERNEL_OUT_DEFAULT)" "$@"
253269

254270
ifeq "$(ALWAYS_BUILD_KERNEL)" "1"
@@ -258,7 +274,7 @@ endif
258274
$(KERNEL_CONFIG_OUT): $(KERNEL_CONFIG) $(KERNEL_DEPS_FLAG)
259275
cp $(KERNEL_CONFIG) $@
260276
sed -i "s|CONFIG_INITRAMFS_SOURCE=.*|CONFIG_INITRAMFS_SOURCE=\"$(INITRAMFS_IN)\"|" $@
261-
MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) olddefconfig
277+
ARCH=$(LINUX_ARCH) MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) olddefconfig
262278

263279
$(KERNEL_PATCH_FLAG): $(realpath $(wildcard $(PATCHES_DIR)/kernel-*))
264280
$(call patch,$(KERNEL_BUILD_DIR),$^)

examples/qemu/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ CROSS_COMPILING := 0
1212
endif
1313

1414
ifeq ($(ARCH), x86_64)
15+
COREBOOT_TOOLCHAIN_ARCH := i386
1516
LINUX_ARCH := $(ARCH)
17+
LINUX_GCC_TUPLE := $(ARCH)-linux-
1618
LINUX_IMAGE_RELATIVE_PATH := arch/x86/boot/bzImage
1719
GOARCH := amd64
1820
PLATFORM := qemu-amd64

tools/build_toolchain.sh

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,41 @@ cleanup
1919
# This will be used by the wget script to record external dependencies while fetching.
2020
export EXTERNAL_DEPS_FILE
2121

22-
if [ -z "${ARCH##x86*}" ] || [ -z "${ARCH##amd64}" ]; then
23-
ARCH=i386
24-
elif [ -z "${ARCH##ppc*}" ]; then
25-
ARCH=ppc64
26-
fi
22+
if [ "$1" == "coreboot" ]; then
23+
if [ -z "${ARCH##x86*}" ] || [ -z "${ARCH##amd64}" ]; then
24+
ARCH=i386
25+
elif [ -z "${ARCH##ppc*}" ]; then
26+
ARCH=ppc64
27+
fi
2728

28-
if ! MAKEFLAGS="" PATH="${TOOLS_DIR}:${PATH}" "${MAKE}" -C "${COREBOOT_BUILD_DIR}" crossgcc-${ARCH} BUILD_LANGUAGES=c; then
29-
{
30-
echo
31-
echo "=== Toolchain build failed"
32-
if [ -f "${EXTERNAL_DEPS_FILE}" ]
33-
then
34-
echo
35-
echo -n "One or more additional external dependencies are required for the build: "
36-
cat "${EXTERNAL_DEPS_FILE}"
37-
echo
38-
echo "Please re-run build with USE_FWDPROXY=1 to collect all the required files."
39-
fi
40-
} >&2
41-
exit 1
29+
if ! MAKEFLAGS="" PATH="${TOOLS_DIR}:${PATH}" "${MAKE}" -C "${COREBOOT_BUILD_DIR}" crossgcc-${ARCH} BUILD_LANGUAGES=c; then
30+
{
31+
echo
32+
echo "=== Toolchain build failed"
33+
if [ -f "${EXTERNAL_DEPS_FILE}" ]
34+
then
35+
echo
36+
echo -n "One or more additional external dependencies are required for the build: "
37+
cat "${EXTERNAL_DEPS_FILE}"
38+
echo
39+
echo "Please re-run build with USE_FWDPROXY=1 to collect all the required files."
40+
fi
41+
} >&2
42+
exit 1
43+
fi
44+
elif [ "$1" == "linux" ]; then
45+
rc=0
46+
pushd .
47+
cd "${COREBOOT_BUILD_DIR}/util/crossgcc"
48+
if ! ./buildgcc -p ${ARCH}-linux -j$(nproc --ignore=1); then
49+
{
50+
echo
51+
echo "=== Toolchain build failed"
52+
rc=1
53+
} >&2
54+
fi
55+
popd
56+
exit $rc
4257
fi
4358

4459
if [ -f "${EXTERNAL_DEPS_FILE}" ]

0 commit comments

Comments
 (0)