summaryrefslogtreecommitdiff
path: root/include/init.sh
blob: 3ca7efeb6fb5862ba6e01e1a7e9511796f1f3649 (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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# SPDX-License-Identifier: GPL-3.0-only

# Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
# Copyright (c) 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
# Copyright (c) 2020-2025 Leah Rowe <leah@libreboot.org>
# Copyright (c) 2025 Alper Nebi Yasak <alpernebiyasak@gmail.com>

export LANG=C.UTF-8
export LC_COLLATE=C.UTF-8
export LC_ALL=C.UTF-8

projectname="libreboot"
projectsite="https://libreboot.org/"

# TODO: consider just erroring here instead of hardcoding a default
if [ -z "${PATH+x}" ]; then
	export PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi

sha512sum="util/sbase/sha512sum"

aur_notice=""
basetmp=""
board=""
checkvarschk=""
checkvarsxbmk=""
configdir=""
datadir=""
is_child=""
python=""
pyver=""
reinstall=""
relname=""
version=""
versiondate=""
xbmklock=""
xbmkpath=""
xbmkpwd=""
xbmkpwd=""
xbtmp=""

xbmk_init()
{
	xbmkpwd="`pwd || err "Cannot generate PWD"`" || err "!" xbmk_init "$@"
	xbmklock="$xbmkpwd/lock"
	basetmp="$xbmkpwd/xbmkwd"
	sha512sum="$xbmkpwd/util/sbase/sha512sum"

	if [ $# -gt 0 ] && [ "$1" = "dependencies" ]; then

		x_ xbmkpkg "$@"
		exit 0
	fi

	id -u 1>/dev/null 2>/dev/null || \
	    err "suid check failed" "xbmk_init" "$@"

	if [ "$(id -u)" = "0" ]; then

		err "this command as root is not permitted" "xbmk_init" "$@"
	fi

	export PWD="$xbmkpwd"
	x_ xbmkdir "$basetmp"

	if [ ! -e "cache" ]; then

		x_ xbmkdir "cache"
	fi

	for init_cmd in get_version set_env set_threads git_init child_exec; do

		if ! xbmk_$init_cmd "$@"; then

			break
		fi
	done
}

xbmkpkg()
{
	xchk xbmkpkg "$@"

	if [ $# -gt 2 ]; then

		reinstall="$3"
	fi

	. "config/dependencies/$2" || \
	    err "Can't read 'config/dependencies/$2'" "xbmkpkg" "$@"

	if [ -z "$pkg_add" ] || \
	    [ -z "$pkglist" ]; then

		err "pkg_add/pkglist not both set" "xbmkpkg" "$@"
	fi

	if [ "$2" == "gentoo" ]; then

		if [ -z "$pkg_use" ] || \
		    [ -z "$pkg_use_file" ]; then

			err "pkg_use/pkg_use_file not both set" "xbmkpkg" "$@"
		fi

		printf "Updating package use...\n"
		printf "Writing into %s" "$pkg_use_file..."

		if [ ! -d "`dirname $pkg_use_file`" ]; then

			x_ mkdir -p "`dirname $pkg_use_file`"
		fi

		printf "%s\n" "$pkg_use" >> "$pkg_use_file"
	fi

	x_ $pkg_add $pkglist

	if [ -n "$aur_notice" ]; then

		printf "You need AUR packages: %s\n" "$aur_notice" 1>&2
	fi
}

xbmk_get_version()
{
	if [ -f ".version" ]; then

		read -r version < ".version" || \
		    err "can't read version file" "xbmk_get_version" "$@"
	fi

	if [ -f ".versiondate" ]; then

		read -r versiondate < ".versiondate" || \
		    err "can't read versiondate" xbmk_get_version "$@"
	fi

	if [ -f ".version" ] && \
	    [ -z "$version" ]; then

		err "version not set" "xbmk_get_version" "$@"
	fi

	if [ -f ".versiondate" ] && \
	    [ -z "$versiondate" ]; then

		err "versiondate not set" "xbmk_get_version" "$@"
	fi

	if [ ! -e ".git" ] && \
	    [ ! -f ".version" ]; then

		version="unknown"
	fi

	if [ ! -e ".git" ] && \
	    [ ! -f ".versiondate" ]; then

		versiondate="1716415872"
	fi

	xbmk_sanitize_version

	if [ -n "$version" ]; then

		relname="$projectname-$version"
	fi
}

# a parent instance will cause this function to return 0.
# a child instance will return 1, skipping further initialisation
# after this function is called.
xbmk_set_env()
{
	is_child="n"

	xbmkpath="$PATH"

	# unify all temporary files/directories in a single TMPDIR

	if [ -n "${TMPDIR+x}" ] && \
	    [ "${TMPDIR%_*}" != "$basetmp/xbmk" ]; then

		unset TMPDIR
	fi

	if [ -n "${TMPDIR+x}" ]; then

		export TMPDIR="$TMPDIR"
		xbtmp="$TMPDIR"
	fi

	if [ -n "${TMPDIR+x}" ]; then

		is_child="y"
	fi

	if [ "$is_child" = "y" ]; then

		# child instance of xbmk, so we stop init after this point
		# and execute the given user command upon return:

		xbmk_child_set_env

		return 1
	else

		# parent instance of xbmk, so we continue initialising.
		# a parent instance of xbmk never processes its own
		# command directly; instead, it calls a child instance
		# of xbmk, and exits with the corresponding return status.

		xbmk_parent_set_env

		return 0
	fi
}

xbmk_child_set_env()
{
	xbmk_child_set_tmp

	if [ -z "${XBMK_CACHE+x}" ]; then

		err "XBMK_CACHE unset on child" "xbmk_set_env" "$@"
	fi

	if [ -z "${XBMK_THREADS+x}" ]; then

		xbmk_set_threads; :
	fi

	if [ -z "${XBMK_CACHE_MIRROR+x}" ]; then

		xbmk_set_mirror
	fi
}

xbmk_child_set_tmp()
{
	badtmp=""
	locktmp=""
	xbtmpchk=""

	xbtmpchk="`findpath "$TMPDIR" || err "!findpath $TMPDIR"`" || \
	    err "!findpath '$TMPDIR'" "xbmk_child_set_tmp" "$@"

	read -r locktmp < "$xbmklock" || \
	    err "can't read '$xbmklock'" "xbmk_child_set_tmp" "$@"

	if [ "$locktmp" != "$xbtmpchk" ]; then

		badtmp="TMPDIR '$xbtmpchk' changed; was '$locktmp'"

		printf "bad TMPDIR init, '%s': %s\n" "$TMPDIR" "$badtmp" 1>&2
		err "'$xbmklock' present with bad tmpdir. is a build running?"
	fi

	xbtmp="$xbtmpchk"

	export TMPDIR="$xbtmpchk"
}

xbmk_parent_set_env()
{
	xbmk_parent_check_tmp

	printf "%s\n" "$xbtmp" > "$xbmklock" || \
	    err "cannot create '$xbmklock'" xbmk_set_env "$@"; :

	# not really critical for security, but it's a barrier
	# against the user to make them think twice before deleting it
	# in case an actual instance of xbmk is already running:

	x_ chmod -w "$xbmklock"

	xbmk_parent_set_export

	xbmk_set_version

	remkdir "$xbtmp" "$xbtmp/gnupath" "$xbtmp/xbmkpath"

	xbmk_set_pyver

	xbmk_set_mirror
}

xbmk_parent_check_tmp()
{
	export TMPDIR="$basetmp"

	xbmklist="`mktemp || err "can't make tmplist"`" || \
	    err "can't make tmplist" xbmk_parent_check_tmp "$@"

	x_ rm -f "$xbmklist"
	x_ touch "$xbmklist"

	for xtmpdir in "$basetmp"/xbmk_*; do

		if [ -e "$xtmpdir" ]; then

			printf "%s\n" "$xtmpdir" >> "$xbmklist" || \
			    err "can't write '$xtmpdir' to '$xbmklist'" \
			    "xbmk_parent_check_tmp" "$@"; :
		fi
	done

	# set up a unified temporary directory, for common deletion later:

	export TMPDIR="`x_ mktemp -d -t xbmk_XXXXXXXX`" || \
	    err "can't export TMPDIR" "xbmk_parent_check_tmp" "$@"

	xbtmp="$TMPDIR"

	while read -r xtmpdir; do

		if [ "$xtmpdir" = "$xbtmp" ]; then

			err "pre-existing '$xbtmp'" "xbmk_parent_check_tmp" "$@"
		fi

	done < "$xbmklist" || \
	    err "Can't read xbmklist: '$xbmklist'" "xbmk_parent_check_tmp" "$@"

	x_ rm -f "$xbmklist"
}

xbmk_parent_set_export()
{
	export XBMK_CACHE="$xbmkpwd/cache"

	if [ -e "$XBMK_CACHE" ] && \
	    [ ! -d "$XBMK_CACHE" ]; then

		err "cachedir '$XBMK_CACHE' is a file" \
		    "xbmk_parent_set_export" "$@"
	fi

	export PATH="$xbtmp/xbmkpath:$xbtmp/gnupath:$PATH"
	xbmkpath="$PATH"

	# if "y": a coreboot target won't be built if target.cfg says release=n
	# (this is used to exclude certain build targets from releases)

	if [ -z "${XBMK_RELEASE+x}" ]; then

		export XBMK_RELEASE="n"
	fi

	if [ "$XBMK_RELEASE" = "Y" ]; then

		export XBMK_RELEASE="y"
	fi

	if [ "$XBMK_RELEASE" != "y" ]; then

		export XBMK_RELEASE="n"
	fi
}

xbmk_set_threads()
{
	if [ -z "${XBMK_THREADS+x}" ]; then

		export XBMK_THREADS=1
	fi

	if ! expr "X$XBMK_THREADS" : "X-\{0,1\}[0123456789][0123456789]*$" \
	    1>/dev/null 2>/dev/null; then

		export XBMK_THREADS=1
	fi
}

xbmk_set_version()
{
	version_="$version"

	if [ -e ".git" ]; then

		version="$(git describe --tags HEAD 2>&1)" || \
		    version="git-$(git rev-parse HEAD 2>&1)" || \
		    version="$version_"
	fi

	versiondate_="$versiondate"

	if [ -e ".git" ]; then

		versiondate="$(git show --no-patch --no-notes \
		    --pretty='%ct' HEAD)" || versiondate="$versiondate_"
	fi

	if [ -z "$version" ] || \
	    [ -z "$versiondate" ]; then

		err "version and/or versiondate unset" "xbmk_set_version" "$@"
	fi

	update_xbmkver "."

	relname="$projectname-$version"

	export LOCALVERSION="-$projectname-${version%%-*}"
}

xbmk_set_pyver()
{
	pyver="2"
	python="python3"
	pyv="import sys; print(sys.version_info[:])"

	if ! pybin python3 1>/dev/null; then

		python="python"
	fi

	if [ "$python" = "python3" ]; then

		pyver="3"
	fi

	if ! pybin "$python" 1>/dev/null; then

		pyver=""
	fi

	if [ -n "$pyver" ]; then

		"`x_ pybin "$python"`" -c "$pyv" 1>/dev/null \
		    2>/dev/null || \
		    err "Can't detect Python version." "xbmk_set_pyver" "$@"
	fi

	if [ -n "$pyver" ]; then

		pyver="$("$(pybin "$python")" -c "$pyv" | awk '{print $1}')"

		pyver="${pyver#(}"

		pyver="${pyver%,}"
	fi

	if [ "${pyver%%.*}" != "3" ]; then

		err "Bad python version (must by 3.x)" "xbmk_set_pyver" "$@"
	fi

	# set up python in PATH (environmental variable):

	(
		x_ cd "$xbtmp/xbmkpath"

		x_ ln -s "`x_ pybin "$python"`" python || \
		    err "can't make symlink" "xbmk_set_pyver" "$@"

	) || \
	    err "Can't link Python in $xbtmp/xbmkpath" "xbmk_set_pyver" "$@"; :
}

# Use direct path, to prevent a hang if Python is using a virtual environment,
# not command -v, to prevent a hang when checking python's version
# See: https://docs.python.org/3/library/venv.html#how-venvs-work
pybin()
{
	py="import sys; quit(1) if sys.prefix == sys.base_prefix else quit(0)"

	venv=1

	if ! command -v "$1" 1>/dev/null 2>/dev/null; then

		venv=0
	fi

	if [ $venv -gt 0 ]; then

		if ! "$1" -c "$py" 1>/dev/null 2>/dev/null; then

			venv=0
		fi
	fi

	# ideally, don't rely on PATH or hardcoded paths if python venv.
	# use the *real*, direct executable linked to by the venv symlink:

	if [ $venv -gt 0 ] && \
	    [ -L "`command -v "$1" 2>/dev/null`" ]; then

		pypath="$(findpath \
		    "$(command -v "$1" 2>/dev/null)" 2>/dev/null || :)"

		if [ -e "$pypath" ] && \
		    [ ! -d "$pypath" ] && \
		    [ -x "$pypath" ]; then

			printf "%s\n" "$pypath"

			return 0
		fi
	fi

	# if python venv: fall back to common PATH directories for checking:

	[ $venv -gt 0 ] && for pypath in "/usr/local/bin" "/usr/bin"; do

		if [ -e "$pypath/$1" ] && \
		     ! -d "$pypath/$1" ] && \
		    [ -x "$pypath/$1" ]; then

			printf "%s/%s\n" "$pypath" "$1"

			return 0
		fi

	done && return 1

	# Defer to normal command -v if not a venv:

	if ! command -v "$1" 2>/dev/null; then

		return 1
	fi
}

xbmk_set_mirror()
{
	# defines whether cache/clone/ (regular clones)
	# or cache/mirror (--mirror clones) are used, per project

	# to use cache/mirror/ do: export XBMK_CACHE_MIRROR="y"
	# mirror/ stores a separate directory per repository, even per backup.
	# it's slower, and uses more disk space, and some upstreams might not
	# appreciate it, so it should only be used for development or archival

	if [ -z "${XBMK_CACHE_MIRROR+x}" ]; then

		export XBMK_CACHE_MIRROR="n"
	fi

	if [ "$XBMK_CACHE_MIRROR" != "y" ]; then

		export XBMK_CACHE_MIRROR="n"
	fi
}

xbmk_git_init()
{
	# the git identity check is only needed for: ./mk -b coreboot [args]

	[ "${2-}" = "-b" ] && [ "${3-}" = "coreboot" ] && \
	    for gitarg in "user.name" "user.email"; do

		gitcmd="git config --includes $gitarg"

		if ! $gitcmd 1>/dev/null 2>/dev/null; then

			err "Run this first: $gitcmd \"your ${gitcmd##*.}\"" \
			    "xbmk_git_init" "$@"
		fi
	done

	if [ -L ".git" ]; then

		err "'$xbmkpwd/.git' is a symlink" "xbmk_git_init" "$@"
	fi

	if [ -e ".git" ]; then

		return 0
	fi

	# GNU-specific extensions of date are used.
	# TODO: that is a bug. fix it!

	x_ date --version | grep "GNU coreutils" 1>/dev/null 2>/dev/null || \
	    err "Non-GNU date implementation" "xbmk_git_init" "$@"

	cdate="`x_ date -Rud @$versiondate || err "can't get date"`" || \
	    err "can't get date" "xbmk_git_init" "$@"

	x_ git init 1>/dev/null 2>/dev/null

	x_ git add -A . 1>/dev/null 2>/dev/null

	x_ git -c user.name="xbmk" -c user.email="xbmk@example.com" \
	    commit -m "$projectname $version" --date "$cdate" \
	    --author="xbmk <xbmk@example.com>" 1>/dev/null 2>/dev/null

	x_ git -c user.name="xbmk" -c user.email="xbmk@example.com" \
	    tag -a "$version" -m "$projectname $version" \
	    1>/dev/null 2>/dev/null; :
}

xbmk_child_exec()
{
	xbmk_rval=0

	( x_ ./mk "$@" ) \
	    || xbmk_rval=1

	( x_ rm -Rf "$xbtmp" ) \
	    || xbmk_rval=1

	( x_ rm -f "$xbmklock" ) \
	    || xbmk_rval=1

	exit $xbmk_rval
}

xbmk_init "$@"