1 |
Introduce a new eclass that contains common logic for building |
2 |
distribution kernels from source. |
3 |
|
4 |
Signed-off-by: Michał Górny <mgorny@g.o> |
5 |
--- |
6 |
eclass/kernel-build.eclass | 175 +++++++++++++++++++++++++++++++++++++ |
7 |
1 file changed, 175 insertions(+) |
8 |
create mode 100644 eclass/kernel-build.eclass |
9 |
|
10 |
Changed in v2: improved cross support, thanks to floppym |
11 |
|
12 |
diff --git a/eclass/kernel-build.eclass b/eclass/kernel-build.eclass |
13 |
new file mode 100644 |
14 |
index 000000000000..028f0da8148e |
15 |
--- /dev/null |
16 |
+++ b/eclass/kernel-build.eclass |
17 |
@@ -0,0 +1,175 @@ |
18 |
+# Copyright 2020 Gentoo Authors |
19 |
+# Distributed under the terms of the GNU General Public License v2 |
20 |
+ |
21 |
+# @ECLASS: kernel-build.eclass |
22 |
+# @MAINTAINER: |
23 |
+# Distribution Kernel Project <dist-kernel@g.o> |
24 |
+# @AUTHOR: |
25 |
+# Michał Górny <mgorny@g.o> |
26 |
+# @SUPPORTED_EAPIS: 7 |
27 |
+# @BLURB: Build mechanics for Distribution Kernels |
28 |
+# @DESCRIPTION: |
29 |
+# This eclass provides the logic to build a Distribution Kernel from |
30 |
+# source and install it. Post-install and test logic is inherited |
31 |
+# from kernel-install.eclass. |
32 |
+# |
33 |
+# The ebuild must take care of unpacking the kernel sources, copying |
34 |
+# an appropriate .config into them (e.g. in src_prepare()) and setting |
35 |
+# correct S. The eclass takes care of respecting savedconfig, building |
36 |
+# the kernel and installing it along with its modules and subset |
37 |
+# of sources needed to build external modules. |
38 |
+ |
39 |
+if [[ ! ${_KERNEL_BUILD_ECLASS} ]]; then |
40 |
+ |
41 |
+case "${EAPI:-0}" in |
42 |
+ 0|1|2|3|4|5|6) |
43 |
+ die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}" |
44 |
+ ;; |
45 |
+ 7) |
46 |
+ ;; |
47 |
+ *) |
48 |
+ die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}" |
49 |
+ ;; |
50 |
+esac |
51 |
+ |
52 |
+inherit savedconfig toolchain-funcs kernel-install |
53 |
+ |
54 |
+BDEPEND=" |
55 |
+ sys-devel/bc |
56 |
+ virtual/libelf" |
57 |
+ |
58 |
+# @FUNCTION: kernel-build_src_configure |
59 |
+# @DESCRIPTION: |
60 |
+# Prepare the toolchain for building the kernel, get the default .config |
61 |
+# or restore savedconfig, and get build tree configured for modprep. |
62 |
+kernel-build_src_configure() { |
63 |
+ debug-print-function ${FUNCNAME} "${@}" |
64 |
+ |
65 |
+ # force ld.bfd if we can find it easily |
66 |
+ local LD="$(tc-getLD)" |
67 |
+ if type -P "${LD}.bfd" &>/dev/null; then |
68 |
+ LD+=.bfd |
69 |
+ fi |
70 |
+ |
71 |
+ tc-export_build_env |
72 |
+ MAKEARGS=( |
73 |
+ V=1 |
74 |
+ |
75 |
+ HOSTCC="$(tc-getBUILD_CC)" |
76 |
+ HOSTCXX="$(tc-getBUILD_CXX)" |
77 |
+ HOSTCFLAGS="${BUILD_CFLAGS}" |
78 |
+ HOSTLDFLAGS="${BUILD_LDFLAGS}" |
79 |
+ |
80 |
+ CROSS_COMPILE=${CHOST}- |
81 |
+ AS="$(tc-getAS)" |
82 |
+ CC="$(tc-getCC)" |
83 |
+ LD="${LD}" |
84 |
+ AR="$(tc-getAR)" |
85 |
+ NM="$(tc-getNM)" |
86 |
+ STRIP=":" |
87 |
+ OBJCOPY="$(tc-getOBJCOPY)" |
88 |
+ OBJDUMP="$(tc-getOBJDUMP)" |
89 |
+ |
90 |
+ # we need to pass it to override colliding Gentoo envvar |
91 |
+ ARCH=$(tc-arch-kernel) |
92 |
+ ) |
93 |
+ |
94 |
+ [[ -f .config ]] || die "Ebuild error: please copy default config into .config" |
95 |
+ restore_config .config |
96 |
+ |
97 |
+ mkdir -p "${WORKDIR}"/modprep || die |
98 |
+ mv .config "${WORKDIR}"/modprep/ || die |
99 |
+ emake O="${WORKDIR}"/modprep "${MAKEARGS[@]}" olddefconfig |
100 |
+ emake O="${WORKDIR}"/modprep "${MAKEARGS[@]}" modules_prepare |
101 |
+ cp -pR "${WORKDIR}"/modprep "${WORKDIR}"/build || die |
102 |
+} |
103 |
+ |
104 |
+# @FUNCTION: kernel-build_src_compile |
105 |
+# @DESCRIPTION: |
106 |
+# Compile the kernel sources. |
107 |
+kernel-build_src_compile() { |
108 |
+ debug-print-function ${FUNCNAME} "${@}" |
109 |
+ |
110 |
+ emake O="${WORKDIR}"/build "${MAKEARGS[@]}" all |
111 |
+} |
112 |
+ |
113 |
+# @FUNCTION: kernel-build_src_test |
114 |
+# @DESCRIPTION: |
115 |
+# Test the built kernel via qemu. This just wraps the logic |
116 |
+# from kernel-install.eclass with the correct paths. |
117 |
+kernel-build_src_test() { |
118 |
+ debug-print-function ${FUNCNAME} "${@}" |
119 |
+ |
120 |
+ emake O="${WORKDIR}"/build "${MAKEARGS[@]}" \ |
121 |
+ INSTALL_MOD_PATH="${T}" modules_install |
122 |
+ |
123 |
+ kernel-install_test "${PV}" \ |
124 |
+ "${WORKDIR}/build/$(kernel-install_get_image_path)" \ |
125 |
+ "${T}/lib/modules/${PV}" |
126 |
+} |
127 |
+ |
128 |
+# @FUNCTION: kernel-build_src_install |
129 |
+# @DESCRIPTION: |
130 |
+# Install the built kernel along with subset of sources |
131 |
+# into /usr/src/linux-${PV}. Install the modules. Save the config. |
132 |
+kernel-build_src_install() { |
133 |
+ debug-print-function ${FUNCNAME} "${@}" |
134 |
+ |
135 |
+ # do not use 'make install' as it behaves differently based |
136 |
+ # on what kind of installkernel is installed |
137 |
+ emake O="${WORKDIR}"/build "${MAKEARGS[@]}" \ |
138 |
+ INSTALL_MOD_PATH="${ED}" modules_install |
139 |
+ |
140 |
+ # note: we're using mv rather than doins to save space and time |
141 |
+ # install main and arch-specific headers first, and scripts |
142 |
+ local kern_arch=$(tc-arch-kernel) |
143 |
+ dodir "/usr/src/linux-${PV}/arch/${kern_arch}" |
144 |
+ mv include scripts "${ED}/usr/src/linux-${PV}/" || die |
145 |
+ mv "arch/${kern_arch}/include" \ |
146 |
+ "${ED}/usr/src/linux-${PV}/arch/${kern_arch}/" || die |
147 |
+ |
148 |
+ # remove everything but Makefile* and Kconfig* |
149 |
+ find -type f '!' '(' -name 'Makefile*' -o -name 'Kconfig*' ')' \ |
150 |
+ -delete || die |
151 |
+ find -type l -delete || die |
152 |
+ cp -p -R * "${ED}/usr/src/linux-${PV}/" || die |
153 |
+ |
154 |
+ cd "${WORKDIR}" || die |
155 |
+ # strip out-of-source build stuffs from modprep |
156 |
+ # and then copy built files as well |
157 |
+ find modprep -type f '(' \ |
158 |
+ -name Makefile -o \ |
159 |
+ -name '*.[ao]' -o \ |
160 |
+ '(' -name '.*' -a -not -name '.config' ')' \ |
161 |
+ ')' -delete || die |
162 |
+ rm modprep/source || die |
163 |
+ cp -p -R modprep/. "${ED}/usr/src/linux-${PV}"/ || die |
164 |
+ |
165 |
+ # install the kernel and files needed for module builds |
166 |
+ insinto "/usr/src/linux-${PV}" |
167 |
+ doins build/{System.map,Module.symvers} |
168 |
+ local image_path=$(kernel-install_get_image_path) |
169 |
+ cp -p "build/${image_path}" "${ED}/usr/src/linux-${PV}/${image_path}" || die |
170 |
+ |
171 |
+ # strip empty directories |
172 |
+ find "${D}" -type d -empty -exec rmdir {} + || die |
173 |
+ |
174 |
+ # fix source tree and build dir symlinks |
175 |
+ dosym ../../../usr/src/linux-${PV} /lib/modules/${PV}/build |
176 |
+ dosym ../../../usr/src/linux-${PV} /lib/modules/${PV}/source |
177 |
+ |
178 |
+ save_config build/.config |
179 |
+} |
180 |
+ |
181 |
+# @FUNCTION: kernel-build_pkg_postinst |
182 |
+# @DESCRIPTION: |
183 |
+# Combine postinst from kernel-install and savedconfig eclasses. |
184 |
+kernel-build_pkg_postinst() { |
185 |
+ kernel-install_pkg_postinst |
186 |
+ savedconfig_pkg_postinst |
187 |
+} |
188 |
+ |
189 |
+_KERNEL_BUILD_ECLASS=1 |
190 |
+fi |
191 |
+ |
192 |
+EXPORT_FUNCTIONS src_configure src_compile src_test src_install pkg_postinst |
193 |
-- |
194 |
2.24.1 |