aboutsummaryrefslogtreecommitdiffstats
path: root/main/linux-rpi/APKBUILD
blob: 26302cb4ecff1e8574b17f2ca9596ba5409c3ccc (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

pkgname=linux-rpi
pkgver=5.4.34
case $pkgver in
*.*.*)	_kernver=${pkgver%.*};;
*.*)	_kernver=${pkgver};;
esac
pkgrel=0
pkgdesc="Linux kernel with Raspberry Pi patches"
url=https://github.com/raspberrypi/linux
depends="mkinitfs"
_depends_dev="perl gmp-dev elfutils-dev bash"
makedepends="$_depends_dev sed installkernel bc linux-headers linux-firmware
	bison flex openssl-dev
"
options="!strip !check"
_rpi_repo="git://github.com/raspberrypi/linux.git"
_linux_repo="git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git"
source="https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-$_kernver.tar.xz
	https://cdn.kernel.org/pub/linux/kernel/v5.x/patch-$pkgver.xz
	https://dev.alpinelinux.org/archive/rpi-patches/rpi-$pkgver-alpine.patch

	issue-4973.patch

	config-changes-rpi.armhf
	config-changes-rpi.armv7
	config-changes-rpi.aarch64
	config-changes-rpi2.armhf
	config-changes-rpi2.armv7
	config-changes-rpi4.armv7
	config-changes-rpi4.aarch64
	"
subpackages=""
arch="armhf armv7 aarch64"
license="GPL-2.0"
_flavors=
for _i in $source; do
	case $_i in
	config-*.$CARCH)
		_f=${_i%.$CARCH}
		_f=${_f#config-changes-}
		_flavors="$_flavors ${_f}"
		[ "linux-$_f" != "$pkgname" ] && subpackages="$subpackages linux-${_f}::$CBUILD_ARCH"
		subpackages="$subpackages linux-${_f}-dev:_dev:$CBUILD_ARCH"
		;;
	esac
done

case "$CARCH" in
	aarch64) _carch="arm64" ;;
	arm*) _carch="arm" ;;
esac

HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

prepare() {
	local _patch_failed=
	cd "$srcdir"/linux-$_kernver
	if [ "${pkgver%.0}" = "$pkgver" ]; then
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N
	fi

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/${i##*/}; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# remove localversion from patch if any
	rm -f localversion*

	local flavor=
	for flavor in $_flavors; do
		local builddir="$srcdir"/build-$flavor
		mkdir -p "$builddir"
		echo "-$pkgrel-$flavor" > "$builddir"/localversion-alpine
		_genconfig $flavor
		make -C "$srcdir"/linux-$_kernver \
			O="$builddir" \
			ARCH="$_carch" \
			HOSTCC="$HOSTCC" \
			olddefconfig
		_verifyconfig $flavor
	done
}

# generate config from defconfig and apply local changes.
# config-changes-$flavor.$CARCH holds a list of = delimited
# config command and values used by kernel scripts/config script.
_genconfig() {
	local flavor=$1 defconfig=
	local builddir="$srcdir"/build-$flavor
	local defconfig=
	case $flavor in
		rpi) defconfig=bcmrpi_defconfig
		[ "$CARCH" = "aarch64" ] && defconfig=bcmrpi3_defconfig ;;
		rpi2) defconfig=bcm2709_defconfig ;;
		rpi4) defconfig=bcm2711_defconfig ;;
		*) die "Unknown flavor: $flavor" ;;
	esac

	cp "$srcdir"/linux-$_kernver/arch/$_carch/configs/$defconfig \
		"$builddir"/.config

	while read line; do
		# skip comments
		case "$line" in
			"#"*) continue;;
		esac
		local option=${line%%=*} str=
		local cmd=$(echo $line | cut -d= -f2)
		case "$cmd" in
			y) cmd="enable";;
			n) cmd="disable";;
			m) cmd="module";;
			'"'*) cmd="set-str"; str="${line#*=}";;
			[0-9]*) cmd="set-val"; str="${line#*=}";;
			*) die "Command $cmd not accepted" ;;
		esac
		msg "[$flavor] $cmd: $option $str"
		"$srcdir"/linux-$_kernver/scripts/config \
			--file "$builddir"/.config \
			--${cmd} "$option" "${str//\"/}"
	done < "$srcdir"/config-changes-$flavor.${CARCH}
}

# verify if options are set to correct value
_verifyconfig() {
	local flavor=$1
	local builddir="$srcdir"/build-$flavor
	while read line; do
		[ ${line:0:1} = "#" ] && continue
		local option=${line%%=*} str= invert=
		local cmd=$(echo $line | cut -d= -f2)
		case "$cmd" in
			enable) str="$option=y" ;;
			disable) str="$option"; invert="-v" ;;
			module) str="$option=m" ;;
			set-val) str="$option=${line##*=}" ;;
			set-str) str=${line##*=}
				str="$option=\"${str//\"/}\"" ;;
		esac
		grep -q $invert "^$str" "$builddir"/.config || \
			die "Config: $option not properly set!"
	done < "$srcdir"/config-changes-$flavor.${CARCH}
}

build() {
	unset LDFLAGS
	for i in $_flavors; do
		cd "$srcdir"/build-$i
		local _kver=$(make kernelversion)
		if [ "$_kver" != "$pkgver" ]; then
			error "Version in Makefile ($_kver) does not correspond with pkgver ($pkgver)"
			return 1
		fi
		make ARCH="$_carch" CC="${CC:-gcc}" \
			KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	done
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	local _abi_release=${pkgver}-${pkgrel}-${_buildflavor}

	cd "$srcdir"/build-$_buildflavor

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	local _install
	case "$CARCH" in
	arm*)
		_install="zinstall dtbs_install"
		;;
	aarch64)
		_install="install dtbs_install"
		;;
	*)
		_install=install
		;;
	esac

	cd "$srcdir"/build-$_buildflavor
	# modules_install seems to regenerate a defect Modules.symvers. Work
	# around it by backing it up and restore it after modules_install
	cp Module.symvers Module.symvers.backup

	local INSTALL_DTBS_PATH="$_outdir"/boot/dtbs-$_buildflavor
	make -j1 modules_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$INSTALL_DTBS_PATH"
	cp Module.symvers.backup Module.symvers

	rm -f "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf "$_outdir"/lib/firmware

	install -D include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release

	if [ "$CARCH" = "aarch64" ]; then
		mv -f "$INSTALL_DTBS_PATH"/broadcom/*.dtb \
			"$INSTALL_DTBS_PATH"
		rmdir "$INSTALL_DTBS_PATH"/broadcom
	fi
}

genpatch() {
	local RPI_REPO_PATH="$HOME/repositories/linux-rpi"
	msg "Checking out/pulling the Linux kernel git repository.."
	mkdir -p "$RPI_REPO_PATH" && cd "$RPI_REPO_PATH"
	git clone "$_linux_repo" "$RPI_REPO_PATH" 2>/dev/null || git pull
	msg "Fetching raspberry git repository.."
	git remote add rpi "$_rpi_repo" 2>/dev/null || true
	git fetch rpi
	msg "Generating rpi patch: rpi-$pkgver.patch"
	mkdir -p "$srcdir"
	git diff v$pkgver remotes/rpi/rpi-${pkgver%.*}.y > \
		"$srcdir"/rpi-$pkgver.patch
	msg "Sending patch to dev.alpinelinux.org.."
	scp "$srcdir"/rpi-$pkgver.patch \
		dev.alpinelinux.org:/archive/rpi-patches/rpi-$pkgver.patch
	cd "$startdir" && abuild checksum
}

# main flavor installs in $pkgdir
package() {
	depends="$depends linux-firmware-brcm"
	_package rpi "$pkgdir"
}

# subflavors install in $subpkgdir
rpi2() {
	depends="$depends linux-firmware-brcm"
	_package rpi2 "$subpkgdir"
}

rpi4() {
	depends="$depends linux-firmware-brcm"
	_package rpi4 "$subpkgdir"
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local _abi_release=${pkgver}-${pkgrel}-$_flavor
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/build-$_flavor/.config "$dir"/.config
	make -j1 -C "$srcdir"/linux-$_kernver O="$dir" HOSTCC="${CC:-gcc}" \
		syncconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$srcdir"/linux-$_kernver
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"
	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build-$_flavor/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="9f60f77e8ab972b9438ac648bed17551c8491d6585a5e85f694b2eaa4c623fbc61eb18419b2656b6795eac5deec0edaa04547fc6723fbda52256bd7f3486898f  linux-5.4.tar.xz
d4e276f2ae648446651dac5b0547dc42d3d7449ea6d629057f4071d6b9225dd9cd2be6ad01f786114acc4a789a62b59e8bc67b6b1e5480d4b4625258a501f4b4  patch-5.4.34.xz
9c2ed676b4bb505b19a76fdb45b764a6d23ecc1f3d840c906763653bdc27932e49bed63212d9ab618c17ccfc06dcee41d30c7014d004a7f95fc6728f3f8fb689  rpi-5.4.34-alpine.patch
501c91bf2538a18102da59bbccc3097f9c3c90079acc0e946ff075074160c09b8a66934e5ce5470e170f0e4f93d114709a95230367426d0bb7ea02c4bdf4cc9b  issue-4973.patch
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi.armhf
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi.armv7
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi.aarch64
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi2.armhf
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi2.armv7
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi4.armv7
bf827937d72085dcd886b8efdf0c930f825dd0778e1baccc535a78475f855a8d457b13647840e8d6e6d81035b856d9e747bb42b6d56ae4e53b73ed3212fc2f4d  config-changes-rpi4.aarch64"