summaryrefslogtreecommitdiff
path: root/eclass/linux-build.eclass
blob: fcb9b8893114ffe752015b60fae42d646d760f42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# Copyright 1999-2014 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# @ECLASS: linux-build.eclass
# @MAINTAINER:
# Bertrand Jacquin <beber@meleeweb.net>
# @BLURB: Eclass for building kernel
# @DESCRIPTION:
# Build kernel properly

case "${EAPI:-0}" in
	5)
		;;
	*)	die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
		;;
esac

ETYPE="sources"

inherit savedconfig kernel-2
detect_version

EXPORT_FUNCTIONS src_prepare src_compile src_install pkg_postinst

SLOT="${PV%.*}"

DESCRIPTION="Kernel stable sources and build"
HOMEPAGE="http://www.kernel.org"
SRC_URI="${KERNEL_URI} ${GENPATCHES_URI} ${ARCH_URI}"

SRCDIR="${WORKDIR}/linux-${KV_FULL}"
BUILDDIR="${WORKDIR}/build"

KV_EXTRA="${EXTRAVERSION}"
S="${SRCDIR}"

IUSE="build source zlib lzma"
REQUIRED_USE="
	?? ( zlib lzma )
	zlib? ( build )
	lzma? ( build )
	symlink? ( source )
	"

DEPEND="
	sys-devel/bc
	sys-apps/diffutils
	|| (
			sys-apps/kmod[zlib?,lzma?]
			sys-apps/module-init-tools
	   )"
RDEPEND=""

# @FUNCTION: _linux-build_configval
# @INTERNAL
# @DESCRIPTION:
# Request a CONFIG value from .config file
# Return true if set, and define CONFIG_<key>
# Return false if not set
_linux-build_configval() {
	local _v="CONFIG_$1"

	# Export only the needed config value
	# when not already define
	declare -p "${_v}" > /dev/null 2>&1 \
		|| . <(sed -n "/^${_v}=/ p" "${BUILDDIR}/.config")

	case "${!_v}" in
		"") declare "$_v=n" ; export "${_v}" ; return 1 ;;
		*)                    export "${_v}" ; return 0 ;;
	esac
}

_linux-build_src_prepare_build() {
	mkdir "${BUILDDIR}"

	restore_config "${BUILDDIR}/.config"
	if [[ ! -e "${BUILDDIR}/.config" ]] ; then
		ewarn "Generate .config using 'defconfig'"
		emake defconfig "${myopt[@]}"
	fi

	mkdir "${WORKDIR}/boot"

	if _linux-build_configval CMDLINE_BOOL && _linux-build_configval CMDLINE ; then
		einfo "Creating kernel.arg"
		einfo "  CONFIG_CMDLINE='${CONFIG_CMDLINE}'"
		echo "${CONFIG_CMDLINE}" > "${WORKDIR}/boot/kernel.arg"
	fi

	_linux-build_configval MODULES || ewarn "CONFIG_MODULES is not set"
	_linux-build_configval UEVENT_HELPER_PATH \
		&& ewarn "CONFIG_UEVENT_HELPER_PATH should be empty"

	_linux-build_configval FHANDLE || ewarn "CONFIG_FHANDLE is needed for >=sys-fs/udev-210"
	_linux-build_configval NET     || ewarn "CONFIG_NET is needed for >=sys-fs/udev-210"
}

linux-build_src_prepare() {
	bopt=(
		-C "${BUILDDIR}"
		-f "${SRCDIR}/Makefile"
		KBUILD_SRC="${SRCDIR}"
		KCONFIG_CONFIG="${BUILDDIR}/.config"

		KBUILD_BUILD_USER="${KBUILD_BUILD_USER:-$PORTAGE_USERNAME}"
	)

	copt=(
		CC="$(tc-getCC)" HOSTCC="${CBUILD}-gcc" CROSS_COMPILE="${CHOST}-"
		ARCH="$(tc-arch-kernel)" SUBARCH="$(tc-arch-kernel)"
	)

	vopt=(
		EXTRAVERSION="${KV_EXTRA}"
		LOCALVERSION= CONFIG_LOCALVERSION=
		V=1
	)

	myopt=( "${bopt[@]}" "${copt[@]}" "${vopt[@]}" )

	use build && _linux-build_src_prepare_build
}

_linux-build_src_compile_build() {
	einfo "Checking configuration file"
	emake oldconfig "${myopt[@]}" < /dev/null

	if diff -Nu0 "${BUILDDIR}/.config.old" "${BUILDDIR}/.config" \
		| egrep -q '^(\-|\+)CONFIG_' ; then
		eerror "You configuration is not up to date"
		diff -Nu "${BUILDDIR}/.config.old" "${BUILDDIR}/.config"
		die "Bad configuration"
	fi

	einfo "Building kernel"
	emake bzImage "${myopt[@]}"

	if _linux-build_configval MODULES ; then
		einfo "Building modules"
		emake modules "${myopt[@]}"
	fi
}

linux-build_src_compile() {
	use build && _linux-build_src_compile_build
}

_linux-build_src_install_sources() {
	kernel-2_src_install
}

_linux-build_src_install_build() {
	einfo "Installing kernel"
	emake install "${myopt[@]}" \
		INSTALL_PATH="${WORKDIR}/boot"

	insinto "/boot/${KV_FULL}"

	newins "${WORKDIR}/boot/config-${KV_FULL}"     kernel.cfg
	newins "${WORKDIR}/boot/vmlinuz-${KV_FULL}"    kernel.img
	newins "${WORKDIR}/boot/System.map-${KV_FULL}" System.map

	if _linux-build_configval CMDLINE_BOOL && _linux-build_configval CMDLINE ; then
		doins "${WORKDIR}/boot/kernel.arg"
	fi

	if _linux-build_configval MODULES ; then
		einfo "Installing modules"
		emake modules_install "${myopt[@]}" \
			INSTALL_MOD_PATH="${WORKDIR}" \
			INSTALL_MOD_STRIP="${PORTAGE_STRIP_FLAGS}"

		# kmod only support zlib and lzma
		# See libkmod/libkmod-util.c / struct kmod_ext kmod_exts
		if use zlib || use lzma ; then
			local cmp ext

			use zlib && { cmp="gzip -9c" ext="gz" ; }
			use lzma && { cmp="xz -9c"   ext="xz" ; }

			einfo "Compressing modules"
			find "${WORKDIR}/lib/modules" \
				-type f -name "*.ko" \
				| while read ; do
					${cmp} < "${REPLY}" > "${REPLY}.${ext}" \
						|| die "${cmp} ${REPLY} failed"
					rm "${REPLY}"
				done
		fi

		einfo "Generating modules.dep"
		depmod -ae -F "${BUILDDIR}/System.map" -b "${WORKDIR}" "${KV_FULL}" \
			|| die "depmod failed"

		rm "${WORKDIR}/lib/modules/${KV_FULL}/build"
		rm "${WORKDIR}/lib/modules/${KV_FULL}/source"

		insinto /lib
		doins -r "${WORKDIR}/lib/modules"

		# No need to make firmware_install, make modules_install does
		if [[ -e "${WORKDIR}/lib/firmware" ]] ; then
			einfo "Installing firmwares"

			insinto /lib
			doins -r "${WORKDIR}/lib/firmware"
		fi
	fi

	use savedconfig && save_config "${BUILDDIR}/.config"
}

linux-build_src_install() {
	use build  && _linux-build_src_install_build
	use source && _linux-build_src_install_sources
}

linux-build_pkg_postinst() {
	kernel-2_pkg_postinst

	# Bypass kernel-2_pkg_postinst madness
	if ! use symlink && [[ -L "${EROOT}usr/src/linux" ]] ; then
		rm "${EROOT}usr/src/linux"
	fi

	einfo "You may need to:"
	einfo "  make menuconfig ${copt[@]}"
}