public inbox for gentoo-dev@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: gentoo-dev@lists.gentoo.org
Cc: "Michał Górny" <mgorny@gentoo.org>
Subject: [gentoo-dev] [PATCH v2 2/4] kernel-build.eclass: Build logic for dist-kernels
Date: Sun,  5 Jan 2020 07:27:39 +0100	[thread overview]
Message-ID: <20200105062741.82357-2-mgorny@gentoo.org> (raw)
In-Reply-To: <20200105062741.82357-1-mgorny@gentoo.org>

Introduce a new eclass that contains common logic for building
distribution kernels from source.

Signed-off-by: Michał Górny <mgorny@gentoo.org>
---
 eclass/kernel-build.eclass | 175 +++++++++++++++++++++++++++++++++++++
 1 file changed, 175 insertions(+)
 create mode 100644 eclass/kernel-build.eclass

Changed in v2: improved cross support, thanks to floppym

diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass
new file mode 100644
index 000000000000..028f0da8148e
--- /dev/null
+++ b/eclass/kernel-build.eclass
@@ -0,0 +1,175 @@
+# Copyright 2020 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+# @ECLASS: kernel-build.eclass
+# @MAINTAINER:
+# Distribution Kernel Project <dist-kernel@gentoo.org>
+# @AUTHOR:
+# Michał Górny <mgorny@gentoo.org>
+# @SUPPORTED_EAPIS: 7
+# @BLURB: Build mechanics for Distribution Kernels
+# @DESCRIPTION:
+# This eclass provides the logic to build a Distribution Kernel from
+# source and install it.  Post-install and test logic is inherited
+# from kernel-install.eclass.
+#
+# The ebuild must take care of unpacking the kernel sources, copying
+# an appropriate .config into them (e.g. in src_prepare()) and setting
+# correct S.  The eclass takes care of respecting savedconfig, building
+# the kernel and installing it along with its modules and subset
+# of sources needed to build external modules.
+
+if [[ ! ${_KERNEL_BUILD_ECLASS} ]]; then
+
+case "${EAPI:-0}" in
+	0|1|2|3|4|5|6)
+		die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
+		;;
+	7)
+		;;
+	*)
+		die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
+		;;
+esac
+
+inherit savedconfig toolchain-funcs kernel-install
+
+BDEPEND="
+	sys-devel/bc
+	virtual/libelf"
+
+# @FUNCTION: kernel-build_src_configure
+# @DESCRIPTION:
+# Prepare the toolchain for building the kernel, get the default .config
+# or restore savedconfig, and get build tree configured for modprep.
+kernel-build_src_configure() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	# force ld.bfd if we can find it easily
+	local LD="$(tc-getLD)"
+	if type -P "${LD}.bfd" &>/dev/null; then
+		LD+=.bfd
+	fi
+
+	tc-export_build_env
+	MAKEARGS=(
+		V=1
+
+		HOSTCC="$(tc-getBUILD_CC)"
+		HOSTCXX="$(tc-getBUILD_CXX)"
+		HOSTCFLAGS="${BUILD_CFLAGS}"
+		HOSTLDFLAGS="${BUILD_LDFLAGS}"
+
+		CROSS_COMPILE=${CHOST}-
+		AS="$(tc-getAS)"
+		CC="$(tc-getCC)"
+		LD="${LD}"
+		AR="$(tc-getAR)"
+		NM="$(tc-getNM)"
+		STRIP=":"
+		OBJCOPY="$(tc-getOBJCOPY)"
+		OBJDUMP="$(tc-getOBJDUMP)"
+
+		# we need to pass it to override colliding Gentoo envvar
+		ARCH=$(tc-arch-kernel)
+	)
+
+	[[ -f .config ]] || die "Ebuild error: please copy default config into .config"
+	restore_config .config
+
+	mkdir -p "${WORKDIR}"/modprep || die
+	mv .config "${WORKDIR}"/modprep/ || die
+	emake O="${WORKDIR}"/modprep "${MAKEARGS[@]}" olddefconfig
+	emake O="${WORKDIR}"/modprep "${MAKEARGS[@]}" modules_prepare
+	cp -pR "${WORKDIR}"/modprep "${WORKDIR}"/build || die
+}
+
+# @FUNCTION: kernel-build_src_compile
+# @DESCRIPTION:
+# Compile the kernel sources.
+kernel-build_src_compile() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	emake O="${WORKDIR}"/build "${MAKEARGS[@]}" all
+}
+
+# @FUNCTION: kernel-build_src_test
+# @DESCRIPTION:
+# Test the built kernel via qemu.  This just wraps the logic
+# from kernel-install.eclass with the correct paths.
+kernel-build_src_test() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	emake O="${WORKDIR}"/build "${MAKEARGS[@]}" \
+		INSTALL_MOD_PATH="${T}" modules_install
+
+	kernel-install_test "${PV}" \
+		"${WORKDIR}/build/$(kernel-install_get_image_path)" \
+		"${T}/lib/modules/${PV}"
+}
+
+# @FUNCTION: kernel-build_src_install
+# @DESCRIPTION:
+# Install the built kernel along with subset of sources
+# into /usr/src/linux-${PV}.  Install the modules.  Save the config.
+kernel-build_src_install() {
+	debug-print-function ${FUNCNAME} "${@}"
+
+	# do not use 'make install' as it behaves differently based
+	# on what kind of installkernel is installed
+	emake O="${WORKDIR}"/build "${MAKEARGS[@]}" \
+		INSTALL_MOD_PATH="${ED}" modules_install
+
+	# note: we're using mv rather than doins to save space and time
+	# install main and arch-specific headers first, and scripts
+	local kern_arch=$(tc-arch-kernel)
+	dodir "/usr/src/linux-${PV}/arch/${kern_arch}"
+	mv include scripts "${ED}/usr/src/linux-${PV}/" || die
+	mv "arch/${kern_arch}/include" \
+		"${ED}/usr/src/linux-${PV}/arch/${kern_arch}/" || die
+
+	# remove everything but Makefile* and Kconfig*
+	find -type f '!' '(' -name 'Makefile*' -o -name 'Kconfig*' ')' \
+		-delete || die
+	find -type l -delete || die
+	cp -p -R * "${ED}/usr/src/linux-${PV}/" || die
+
+	cd "${WORKDIR}" || die
+	# strip out-of-source build stuffs from modprep
+	# and then copy built files as well
+	find modprep -type f '(' \
+			-name Makefile -o \
+			-name '*.[ao]' -o \
+			'(' -name '.*' -a -not -name '.config' ')' \
+		')' -delete || die
+	rm modprep/source || die
+	cp -p -R modprep/. "${ED}/usr/src/linux-${PV}"/ || die
+
+	# install the kernel and files needed for module builds
+	insinto "/usr/src/linux-${PV}"
+	doins build/{System.map,Module.symvers}
+	local image_path=$(kernel-install_get_image_path)
+	cp -p "build/${image_path}" "${ED}/usr/src/linux-${PV}/${image_path}" || die
+
+	# strip empty directories
+	find "${D}" -type d -empty -exec rmdir {} + || die
+
+	# fix source tree and build dir symlinks
+	dosym ../../../usr/src/linux-${PV} /lib/modules/${PV}/build
+	dosym ../../../usr/src/linux-${PV} /lib/modules/${PV}/source
+
+	save_config build/.config
+}
+
+# @FUNCTION: kernel-build_pkg_postinst
+# @DESCRIPTION:
+# Combine postinst from kernel-install and savedconfig eclasses.
+kernel-build_pkg_postinst() {
+	kernel-install_pkg_postinst
+	savedconfig_pkg_postinst
+}
+
+_KERNEL_BUILD_ECLASS=1
+fi
+
+EXPORT_FUNCTIONS src_configure src_compile src_test src_install pkg_postinst
-- 
2.24.1



  reply	other threads:[~2020-01-05  6:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-05  6:27 [gentoo-dev] [PATCH v2 1/4] kernel-install.eclass: Install logic for dist-kernels Michał Górny
2020-01-05  6:27 ` Michał Górny [this message]
2020-01-06 17:43   ` [gentoo-dev] [PATCH v2 2/4] kernel-build.eclass: Build " Mike Gilbert
2020-01-06 17:54     ` Michał Górny
2020-01-06 18:07       ` Mike Gilbert
2020-01-06 18:13         ` Michał Górny
2020-01-05  6:27 ` [gentoo-dev] [PATCH v2 3/4] sys-kernel/vanilla-kernel: Migrate to kernel-build.eclass Michał Górny
2020-01-05  6:27 ` [gentoo-dev] [PATCH v2 4/4] sys-kernel/vanilla-kernel-bin: Migrate to kernel-install.eclass Michał Górny
2020-01-05  7:30 ` [gentoo-dev] [PATCH v2 1/4] kernel-install.eclass: Install logic for dist-kernels Robin H. Johnson
2020-01-07  6:11   ` Michał Górny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200105062741.82357-2-mgorny@gentoo.org \
    --to=mgorny@gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox