File: envkernel.sh

package info (click to toggle)
pmbootstrap 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,864 kB
  • sloc: python: 17,395; sh: 425; makefile: 17
file content (493 lines) | stat: -rw-r--r-- 11,952 bytes parent folder | download
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/bin/sh
# Copyright 2019 Oliver Smith
# SPDX-License-Identifier: GPL-3.0-or-later
#
# usage example:
# $ cd ~/code/linux
# $ source ~/code/pmbootstrap/helpers/envkernel.sh

check_kernel_folder() {
	[ -e "Kbuild" ] && return
	echo "ERROR: This folder is not a linux source tree: $PWD"
	return 1
}


clean_kernel_src_dir() {
	# Prevent Linux from appending Git version information to kernel version
	# This will cause kernels to be packaged incorrectly.
	touch .scmversion

	if [ -f ".config" ] || [ -d "include/config" ]; then
		echo "Source directory is not clean, running 'make mrproper'."

		tmp_dir=""
		if [ -d ".output" ]; then
			echo " * Preserving existing build output."
			tmp_dir=$(mktemp -d)
			sudo mv ".output" "$tmp_dir"
		fi;

		# backslash is prefixed to disable the alias
		# shellcheck disable=SC1001
		\make mrproper

		if [ -n "$tmp_dir" ]; then
			sudo mv "$tmp_dir/.output" ".output"
			sudo rmdir "$tmp_dir"
		fi;
	fi;
}


export_envkernel_sh() {
	# Get script location
	# See also: <https://stackoverflow.com/a/29835459>
	# shellcheck disable=SC3054
	if [ -n "${BASH_SOURCE[0]}" ]; then
		envkernel_sh="$(realpath "$BASH_SOURCE")"
	else
		envkernel_sh="$1"
	fi
	export envkernel_sh
}


export_pmbootstrap_dir() {
	if [ -n "$pmbootstrap_dir" ]; then
		return 0;
	fi

	# Get pmbootstrap dir based on this script's location, if it's
	# in a pmbootstrap source tree
	pmbootstrap_dir="$(realpath "$(dirname "${envkernel_sh}")/..")"
	if [ -e "$pmbootstrap_dir/pmbootstrap.py" ]; then
		export pmbootstrap_dir
	else
		unset pmbootstrap_dir
	fi
}


set_alias_pmbootstrap() {
	if [ -n "$pmbootstrap_dir" ] \
			&& [ -e "$pmbootstrap_dir/pmbootstrap.py" ]; then
		pmbootstrap="$pmbootstrap_dir"/pmbootstrap.py
		# shellcheck disable=SC2139
		alias pmbootstrap="\"$pmbootstrap\""
	elif [ -n "$(command -v pmbootstrap)" ]; then
		pmbootstrap="$(command -v pmbootstrap)"
	else
		echo "ERROR: pmbootstrap not found!"
		echo "If you're loading envkernel.sh from a pmbootstrap source tree,"
		echo "please check export_pmbootstrap_dir in envkernel.sh. Otherwise "
		echo "please make sure 'pmbootstrap' is on your PATH."
		return 1
	fi

	if [ -e "${XDG_CONFIG_HOME:-$HOME/.config}"/pmbootstrap_v3.cfg ]; then
		"$pmbootstrap" work_migrate
	else
		echo "NOTE: First run of pmbootstrap, running 'pmbootstrap init'"
		"$pmbootstrap" init
	fi
}


export_chroot_device_deviceinfo() {
	chroot="$("$pmbootstrap" config work)/chroot_native"
	device="$("$pmbootstrap" config device)"
	deviceinfo="$(echo "$("$pmbootstrap" config aports)"/device/*/device-"$device"/deviceinfo)"
	export chroot device deviceinfo
}


check_device() {
	[ -e "$deviceinfo" ] && return
	echo "ERROR: Please select a valid device in 'pmbootstrap init'"
	return 1
}


initialize_chroot() {
	# Kernel architecture
	# shellcheck disable=SC2154
	case "$deviceinfo_arch" in
		aarch64*) arch="arm64" ;;
		arm*) arch="arm" ;;
		riscv*) arch="riscv" ;;
		x86_64) arch="x86_64" ;;
		x86) arch="x86" ;;
	esac

	# Check if it's a cross compile
	host_arch="$(uname -m)"
	need_cross_compiler=1
	# Match arm* architectures
	# shellcheck disable=SC3057
	arch_substr="${host_arch:0:3}"
	if [ "$arch" = "$host_arch" ] || \
		{ [ "$arch_substr" = "arm" ] && [ "$arch_substr" = "$arch" ]; } || \
		{ [ "$arch" = "arm64" ] && [ "$host_arch" = "aarch64" ]; } || \
		{ [ "$arch" = "riscv" ] && [ "$host_arch" = "riscv64" ]; } || \
		{ [ "$arch" = "x86" ] && [ "$host_arch" = "x86_64" ]; }; then
		need_cross_compiler=0
	fi

	# LLVM is a cross compiler in itself
	if [ -z "$gcc_arg" ] && [ -z "$gcc6_arg" ] && [ -z "$gcc4_arg" ]; then
		need_cross_compiler=0
		pkgs="clang lld llvm"
		toolchain_name="llvm"
	else
		if [ "$gcc6_arg" = "1" ]; then
			gcc_pkgname="gcc6"
		elif [ "$gcc4_arg" = "1" ]; then
			gcc_pkgname="gcc4"
		elif [ "$gcc_arg" = "1" ]; then
			gcc_pkgname="gcc"
		fi

		cross_binutils=""
		cross_gcc=""
		if [ "$need_cross_compiler" = 1 ]; then
			cross_binutils="binutils-$deviceinfo_arch"
			cross_gcc="$gcc_pkgname-$deviceinfo_arch"
		fi

		pkgs="$cross_binutils $cross_gcc $gcc_pkgname g++"
		toolchain_name="$gcc_pkgname"
	fi

	# Don't initialize twice
	flag="$chroot/tmp/envkernel/${toolchain_name}_setup_done"
	[ -e "$flag" ] && return

	# Install needed packages
	echo "Initializing Alpine chroot (details: 'pmbootstrap log')"

	# FIXME: Ideally we would not "guess" the dependencies here.
	# It might be better to take a kernel package name as parameter
	#   (e.g. . envkernel.sh linux-postmarketos-mainline)
	# and install its build dependencies.

	# If we're running zsh, enable word splitting for this next section. pmbootstrap#2685 workaround.
	if [ -n "${ZSH_VERSION-}" ]; then
		case $(setopt)
			in *shwordsplit*) has_shwordsplit=1 ;;
		esac

		setopt shwordsplit
	fi

	# shellcheck disable=SC2086,SC2154
	"$pmbootstrap" -q chroot -- apk -q add \
		abuild \
		bash \
		bc \
		binutils \
		bison \
		clang-libclang \
		diffutils \
		elfutils-dev \
		findutils \
		flex \
		gmp-dev \
		gnutls-dev \
		linux-headers \
		openssl-dev \
		make \
		mpc1-dev \
		mpfr-dev \
		musl-dev \
		ncurses-dev \
		perl \
		py3-dt-schema \
		rust \
		rust-bindgen \
		rust-src \
		rustfmt \
		sed \
		yamllint \
		yaml-dev \
		xz \
		$pkgs || return 1

	# Disable shwordsplit if it wasn't enabled before, and unset the temporary variable if it was.
	if [ -n "${ZSH_VERSION-}" ]; then
		[ -z "$has_shwordsplit" ] && unsetopt shwordsplit
		[ -n "$has_shwordsplit" ] && unset has_shwordsplit
	fi

	# Create /mnt/linux
	sudo mkdir -p "$chroot/mnt/linux"

	# Mark as initialized
	"$pmbootstrap" -q chroot -- su pmos -c \
		"mkdir /tmp/envkernel; touch /tmp/envkernel/$(basename "$flag")"
}


mount_kernel_source() {
	if [ -e "$chroot/mnt/linux/Kbuild" ]; then
		sudo umount "$chroot/mnt/linux"
	fi
	sudo mount --bind "$PWD" "$chroot/mnt/linux"
}


create_output_folder() {
	[ -d "$chroot/mnt/linux/.output" ] && return
	mkdir -p ".output"
	"$pmbootstrap" -q chroot -- chown -R pmos:pmos "/mnt/linux/.output"
}


set_alias_make() {
	# Cross compiler prefix
	# shellcheck disable=SC1091
	prefix="$(CBUILD="$deviceinfo_arch" . "$chroot/usr/share/abuild/functions.sh";
		arch_to_hostspec "$deviceinfo_arch")"

	if [ "$gcc6_arg" = "1" ]; then
		cc="gcc6-${prefix}-gcc"
		hostcc="gcc6-gcc"
		cross_compiler="/usr/bin/gcc6-$prefix-"
	elif [ "$gcc4_arg" = "1" ]; then
		cc="gcc4-${prefix}-gcc"
		hostcc="gcc4-gcc"
		cross_compiler="/usr/bin/gcc4-$prefix-"
	else
		cc="${prefix}-gcc"
		hostcc="gcc"
		cross_compiler="/usr/bin/$prefix-"
	fi

	if [ "$arch" = "x86" ] && [ "$host_arch" = "x86_64" ]; then
		cc=$hostcc
	fi

	# Build make command
	cmd="echo '*** pmbootstrap envkernel.sh active for $PWD! ***';"
	cmd="$cmd pmbootstrap -q chroot --user --"
	cmd="$cmd CCACHE_DISABLE=1"
	cmd="$cmd ARCH=$arch"
	if [ "$need_cross_compiler" = 1 ]; then
		cmd="$cmd CROSS_COMPILE=$cross_compiler"
	fi
	cmd="$cmd make -C /mnt/linux O=/mnt/linux/.output"
	if [ -z "$gcc_arg" ] && [ -z "$gcc6_arg" ] && [ -z "$gcc4_arg" ]; then
		cmd="$cmd LLVM=1"
	else
		cmd="$cmd CC=$cc HOSTCC=$hostcc"
	fi

	# shellcheck disable=SC2139
	alias make="$cmd"
	unset cmd

	# Build run-script command
	cmd="_run_script() {"
	cmd="$cmd echo '*** pmbootstrap envkernel.sh active for $PWD! ***';"
	cmd="$cmd _script=\"\$1\";"
	cmd="$cmd shift;"
	cmd="$cmd if [ -e \"\$_script\" ]; then"
	cmd="$cmd 	echo \"Running \$_script in the chroot native /mnt/linux/\";"
	cmd="$cmd 	pmbootstrap -q chroot --user -- sh -c \"cd /mnt/linux;"
	cmd="$cmd 	srcdir=/mnt/linux builddir=/mnt/linux/.output tmpdir=/tmp/envkernel"
	cmd="$cmd 	./\"\$_script\" \\\"\\\$@\\\"\" \"sh\" \"\$@\";"
	cmd="$cmd else"
	cmd="$cmd 	echo \"ERROR: \$_script not found.\";"
	cmd="$cmd fi;"
	cmd="$cmd };"
	cmd="$cmd _run_script \"\$@\""
	# shellcheck disable=SC2139
	alias run-script="$cmd"
	unset cmd
}


set_alias_pmbroot_kernelroot() {
	if [ -n "$pmbootstrap_dir" ]; then
		# shellcheck disable=SC2139
		alias pmbroot="cd '$pmbootstrap_dir'"
	fi
	# shellcheck disable=SC2139
	alias kernelroot="cd '$PWD'"
}


cross_compiler_version() {
	if [ "$need_cross_compiler" = 1 ]; then
		"$pmbootstrap" chroot --user -- "${cross_compiler}gcc"  --version \
			2> /dev/null | grep "^.*gcc " | \
			awk -F'[()]' '{ print $1 "("$2")" }'
	else
		echo "none"
	fi
}


update_prompt() {
	if [ -n "$ZSH_VERSION" ]; then
		# assume Zsh
		export _OLD_PROMPT="$PROMPT"
		export PROMPT="[envkernel] $PROMPT"
	elif [ -n "$BASH_VERSION" ]; then
		export _OLD_PS1="$PS1"
		export PS1="[envkernel] $PS1"
	fi
}


set_deactivate() {
	cmd="_deactivate() {"
	cmd="$cmd unset POSTMARKETOS_ENVKERNEL_ENABLED;"
	if [ -n "$pmbootstrap_dir" ]; then
		cmd="$cmd unalias pmbootstrap pmbroot;"
	fi
	cmd="$cmd unalias make kernelroot run-script;"
	cmd="$cmd unalias deactivate reactivate;"
	cmd="$cmd unset pmbootstrap pmbootstrap_dir;"
	cmd="$cmd if [ -n \"\$_OLD_PS1\" ]; then"
	cmd="$cmd   export PS1=\"\$_OLD_PS1\";"
	cmd="$cmd   unset _OLD_PS1;"
	cmd="$cmd elif [ -n \"\$_OLD_PROMPT\" ]; then"
	cmd="$cmd   export PROMPT=\"\$_OLD_PROMPT\";"
	cmd="$cmd   unset _OLD_PROMPT;"
	cmd="$cmd fi"
	cmd="$cmd };"
	cmd="$cmd _deactivate \"\$@\""
	# shellcheck disable=SC2139
	alias deactivate="$cmd"
	unset cmd
}

set_reactivate() {
	# shellcheck disable=SC2139
	alias reactivate="deactivate; pushd '$PWD'; . '$envkernel_sh'; popd"
}

check_and_deactivate() {
	if [ "$POSTMARKETOS_ENVKERNEL_ENABLED" = 1 ]; then
		# we already are running in envkernel
		deactivate
	fi
}


print_usage() {
	# shellcheck disable=SC3054
	if [ -n "${BASH_SOURCE[0]}" ]; then
		echo "usage: source $(basename "$(realpath "$BASH_SOURCE")")"
	fi
	echo "optional arguments:"
	echo "    --fish        Print fish alias syntax (internally used)"
	echo "    --gcc6        Use GCC6 cross compiler"
	echo "    --gcc4        Use GCC4 cross compiler"
	echo "    --gcc         Use GCC cross compiler"
	echo "    --help        Show this help message"
}


parse_args() {
	unset fish_arg
	unset gcc6_arg
	unset gcc4_arg
	unset gcc_arg

	while [ "${1:-}" != "" ]; do
		case $1 in
		--fish)
			fish_arg="$1"
			shift
			;;
		--gcc6)
			gcc6_arg=1
			shift
			;;
		--gcc4)
			gcc4_arg=1
			shift
			;;
		--gcc)
			gcc_arg=1
			shift
			;;
		--help)
			shift
			return 0
			;;
		*)
			echo "Invalid argument: $1"
			shift
			return 0
			;;
		esac
	done

	return 1
}


main() {
	# Stop executing once a function fails
	# shellcheck disable=SC1090
	if check_and_deactivate \
		&& check_kernel_folder \
		&& clean_kernel_src_dir \
		&& export_envkernel_sh "$1" \
		&& export_pmbootstrap_dir \
		&& set_alias_pmbootstrap \
		&& export_chroot_device_deviceinfo \
		&& check_device \
		&& . "$deviceinfo" \
		&& initialize_chroot \
		&& mount_kernel_source \
		&& create_output_folder \
		&& set_alias_make \
		&& set_alias_pmbroot_kernelroot \
		&& update_prompt \
		&& set_deactivate \
		&& set_reactivate; then

		POSTMARKETOS_ENVKERNEL_ENABLED=1

		# Success
		echo "pmbootstrap envkernel.sh activated successfully."
		echo " * kernel source:  $PWD"
		echo " * output folder:  $PWD/.output"
		echo " * architecture:   $arch ($device is $deviceinfo_arch)"
		echo " * cross compile:  $(cross_compiler_version)"
		if [ -n "$pmbootstrap_dir" ]; then
			echo " * aliases: make, kernelroot, pmbootstrap, pmbroot," \
				"run-script (see 'type make' etc.)"
		else
			echo " * aliases: make, kernelroot, run-script"\
				"(see 'type make' etc.)"
		fi
		echo " * run 'deactivate' to revert all env changes"
	else
		# Failure
		echo "See also: <https://postmarketos.org/troubleshooting>"
		return 1
	fi
}


# Print fish alias syntax (when called from envkernel.fish)
fish_compat() {
	[ "$1" = "--fish" ] || return 0
	for name in make kernelroot pmbootstrap pmbroot; do
		alias "$name" >/dev/null 2>&1 && echo "alias $(alias "$name" | sed 's/=/ /')"
	done
}

if parse_args "$@"; then
	print_usage "$0"
	return 1
fi

# Run main() with all output redirected to stderr
# Afterwards print fish compatible syntax to stdout
main "$0" >&2 && fish_compat "$fish_arg"