Skip to content

Commit cba5ee4

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 cba5ee4

3 files changed

Lines changed: 59 additions & 25 deletions

File tree

Makefile.inc

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,15 @@ 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)
123126
else
127+
BUILD_LINUX_WITH_COREBOOT_TOOLCHAIN = 0
128+
CROSS_COMPILE ?= $(LINUX_GCC_TUPLE)
129+
endif
130+
else
131+
CROSS_COMPILE =
124132
# FIXME: Also need to fix behavior when we're not cross-compilng, but system's
125133
# go version doesn't match the version we're using to build the initramfs...
126134
#INITRAMFS_BUILD_GOROOT ?= $(INITRAMFS_BUILD_DIR)/go
@@ -201,8 +209,8 @@ $(COREBOOT_TOOLCHAIN_OUT)/bin/iasl: $(COREBOOT_DEPS_FLAG) $(COREBOOT_PATCH_FLAG)
201209
tar xf $(COREBOOT_TOOLCHAIN_CACHE) -C $(dir $(COREBOOT_TOOLCHAIN_OUT)); \
202210
touch $@; \
203211
else \
204-
COREBOOT_BUILD_DIR=$(COREBOOT_BUILD_DIR) CPUS=$(NPROC) MAKE=$(MAKE) ARCH=$(ARCH) TOOLS_DIR=$(TOOLS_DIR) \
205-
$(TOOLS_DIR)/build_toolchain.sh; \
212+
COREBOOT_BUILD_DIR=$(COREBOOT_BUILD_DIR) CPUS=$(NPROC) MAKE=$(MAKE) ARCH=$(COREBOOT_TOOLCHAIN_ARCH) \
213+
TOOLS_DIR=$(TOOLS_DIR) $(TOOLS_DIR)/build_toolchain.sh coreboot; \
206214
fi
207215

208216
# Creates coreboot config file using the user-provided one and feeding it to make olddefconfig.
@@ -236,19 +244,28 @@ wipe-coreboot:
236244
# Top-level initramfs target, for use by humans.
237245
kernel: $(KERNEL_OUT)
238246

247+
kernel_toolchain: $(COREBOOT_DEPS_FLAG) $(COREBOOT_PATCH_FLAG)
248+
ifeq "$(BUILD_LINUX_WITH_COREBOOT_TOOLCHAIN)" "1"
249+
echo 'Building cross toolchain for Linux'
250+
COREBOOT_BUILD_DIR=$(COREBOOT_BUILD_DIR) CPUS=$(NPROC) MAKE=$(MAKE) ARCH=$(LINUX_ARCH) \
251+
TOOLS_DIR=$(TOOLS_DIR) $(TOOLS_DIR)/build_toolchain.sh linux;
252+
else
253+
echo 'Building Linux with toolchain tuple: $(CROSS_COMPILE)'
254+
endif
255+
239256
# Kernel build target. Requires initrmafs image, config and patches.
240257
ifneq "$(INITRAMFS_IN)" ""
241-
$(KERNEL_OUT): $(INITRAMFS_IN) $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG)
258+
$(KERNEL_OUT): $(INITRAMFS_IN) $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG) kernel_toolchain
242259
else
243-
$(KERNEL_OUT): $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG)
260+
$(KERNEL_OUT): $(KERNEL_CONFIG_OUT) $(KERNEL_PATCH_FLAG) $(KERNEL_TOOLCHAIN) $(KERNEL_TOOLCHAIN)
244261
endif
245262
@# If lzma(1) is not available, kernel build will fall back to gzip and we don't want that.
246263
@if ! lzma -h > /dev/null 2>/dev/null; then \
247264
echo ' *** Please install the lzma CLI utility.' >&2; \
248265
echo ' *** In RedHat distros it`s provided by xz-lzma-compat, in Debian/Ubuntu it`s provided by xz-utils.' >&2; \
249266
exit 1; \
250267
fi
251-
MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) -j$(NPROC) KCFLAGS=-pipe
268+
ARCH=$(LINUX_ARCH) CROSS_COMPILE=$(CROSS_COMPILE) MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) -j$(NPROC) KCFLAGS=-pipe
252269
[ "$@" = "$(KERNEL_OUT_DEFAULT)" ] || cp -v "$(KERNEL_OUT_DEFAULT)" "$@"
253270

254271
ifeq "$(ALWAYS_BUILD_KERNEL)" "1"
@@ -258,7 +275,7 @@ endif
258275
$(KERNEL_CONFIG_OUT): $(KERNEL_CONFIG) $(KERNEL_DEPS_FLAG)
259276
cp $(KERNEL_CONFIG) $@
260277
sed -i "s|CONFIG_INITRAMFS_SOURCE=.*|CONFIG_INITRAMFS_SOURCE=\"$(INITRAMFS_IN)\"|" $@
261-
MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) olddefconfig
278+
ARCH=$(LINUX_ARCH) MAKEFLAGS= $(MAKE) -C $(KERNEL_BUILD_DIR) olddefconfig
262279

263280
$(KERNEL_PATCH_FLAG): $(realpath $(wildcard $(PATCHES_DIR)/kernel-*))
264281
$(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)