summaryrefslogtreecommitdiff
path: root/mk
blob: 036b6714fea09a85bd2b2024edca88842dc04a52 (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
#!/bin/sh

# SPDX-License-Identifier: GPL-3.0-or-later

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

set -u -e

findpath()
{
	forx 'readlink -f \"%s\" 2>/dev/null || \
	    x_ realpath \"%s\" 2>/dev/null' "$@"
}

printf "%s\n" "%s" | grep -o "%s" 1>/dev/null || exit 1 # safety (-o check)
forx()
{
	awkarg="{printf \"${1-};\""
	shift 1 2>/dev/null || :

	scount="$(printf "%s\n" "$awkarg" | grep -o "%s" | wc -l)"
	while [ $scount -gt 0 ]; do
		awkarg="$awkarg,\$0"
		scount="$(expr $scount - 1 || :)"
	done

	while [ $# -gt 0 ]; do
		eval "$(printf '%s\n' "$1" | awk "$awkarg}")"
		shift 1
	done
}

x_()
{
	[ $# -lt 1 ] || "$@" || err "UNHANDLED ERROR" "x_" "$@"; :
}

err()
{
	printf '%s\n' "$@" | awk '{printf "\"%s\" ", $0}'
	printf "ERROR!\n"

	exit 1
}

forx 'export %s=\"C.UTF-8\"' LANG LANGUAGE LC_CTYPE LC_NUMERIC LC_TIME \
    LC_COLLATE LC_MONETARY LC_MESSAGES LC_PAPER LC_NAME LC_ADDRESS \
    LC_TELEPHONE LC_MEASUREMENT LC_IDENTIFICATION LC_ALL

(
mk="$0"
[ "${mk##*/}" != "$mk" ] || \
    mk="$(x_ command -v "$mk")" || exit 1

mk="$(x_ findpath "$mk")" || exit 1
xbmkpwd="$(x_ dirname "$mk")" || exit 1

x_ cd "$xbmkpwd"

. "include/lib.sh"
. "include/env/init.sh"
x_ xbmk_init "$@"

. "include/env/get.sh"

. "include/fw/vendor.sh"
. "include/fw/mrc.sh"
. "include/fw/inject.sh"
. "include/fw/rom.sh"

. "include/mk/release.sh"

_f="${1-}"
shift 1 2>/dev/null || :

eval "$(printf "version release download inject\n" | awk '{ for (i=1; i<=NF; \
    i++) $i = "[ \""$i"\" = \"$_f\" ] && x_ "$i" \"$@\" && exit 0;"; print }')"

# macros, overridden depending on the flag
if_make_config=":"
if_build=":" # can be false even if make is true
if_dry_build=":"
eval "`newvar if_not_make_config if_not_build if_not_dry_build if_not_fetch`"

# these variables will also be initialised in configure_target
cfgvars="autogenargs cmakedir configureargs badhash badtghash bootstrapargs \
    build_depend cleanargs fetch_depend makeargs mkhelper noconfig postmake \
    premake release rev tree xarch xgcctree xlang cfgname defscan \
    if_not_skip_build"

eval "`newvar buildcfg dest_dir elfdir flag forcepull listfile mdir \
    mkhelpercfg mode project srcdir target target_dir _tcfg \
    $cfgvars`"

# when saved to e.g. coreboot src. can be overridden by projects
cfgname_default=".config" # e.g. src/coreboot/default/.config

main()
{
	flags="f:F:b:m:u:s:l:n:d:"

	while getopts $flags option
	do
		[ -n "$flag" ] && \
			err "only one flag is permitted" "trees" "$@"

		flag="$1"

		# the "mode" variable is affixed to a make command, example:
		# "$mk" -m coreboot does: make menuconfig -C src/coreboot/tree

		case "$flag" in
		-b) : ;; # build a source tree
		-d) # dry builds. -d is like -b but skips many build steps
			if_dry_build=""
			if_not_dry_build=":"
			;;
		-f|-F) # download source code for a project
			if_not_fetch=":"
			if_dry_build=""
			if_not_dry_build=":"
			[ "$flag" = "-F" ] && \
				forcepull="y"; : # never skip git fetch/pull
			;;
		-u) mode="oldconfig" ;;
		-m) mode="menuconfig" ;;
		-s) mode="savedefconfig" ;;
		-l) mode="olddefconfig" ;;
		-n) mode="nconfig" ;;
		*) err "invalid option '-$option'" "trees" "$@" ;;
		esac

		if [ -z "$mode" ]; then
			if_build=""
			if_not_build=":"
		fi
		if [ "${mode%config}" != "$mode" ]; then
			if_make_config=""
			if_not_make_config=":"
		fi

		if [ -z "${OPTARG+x}" ]; then
			shift 1
			break
		fi

		# user can type e.g. ./mk -f src/flashprog
		project="${OPTARG#src/}"
		project="${project#config/module/}"

		shift 2
	done

	[ -z "$flag" ] && err "missing flag ($flags)" "trees" "$@"
	[ -z "$project" ] && \
		x_ fx_ "x_ "$mk" $flag" x_ find "config/module" \
		    -mindepth 1 -maxdepth 1 -type d && return 0
	[ ! -d "config/module/$project" ] && \
		err "module '$project' not found" main "$flag" "" "$@"

	datadir="config/data/$project"
	configdir="config/module/$project"
	[ "${1-}" = "list" ] && \
		x_ ls -1 "$configdir" && \
		return 0

	listfile="$datadir/build.list"
	[ ! -f "$listfile" ] && \
		listfile="" # TODO: override listfile per-tree

	mkhelpercfg="$datadir/mkhelper.cfg"
	e "$mkhelpercfg" f missing && \
		mkhelpercfg="$xbtmp/mkhelper.cfg"
	x_ touch "$mkhelpercfg"

	remkdir "${tmpgit%/*}"
	x_ touch "$mkhelpercfg"

	[ ! -d "$configdir" ] && err "missing '$configdir'" handle_target "$@"
	[ $# -lt 1 ] && fx_ handle_target find "$configdir" \
		    -mindepth 1 -maxdepth 1 -type d

	while [ $# -gt 0 ]; do
		handle_target "$1"
		shift 1
	done
}

handle_target()
{
	unset CROSS_COMPILE
	export PATH="$xbmkpath"

	printf "'make %s', '%s', '%s'\n" "$mode" "$project" "$1"

	target="${1##*/}"
	target_dir="$configdir/$target"
	defscan="$target_dir/config"

	configure_target "$target_dir" && \
		$if_not_fetch x_ handle_buildcfg

	$if_build $if_not_fetch \
		x_ $postmake; :
}

configure_target()
{
	_tcfg="$1/target.cfg"

	eval "`newvar $cfgvars`"

	. "$mkhelpercfg" # mkhelper reinitialisation

	# override target/tree specific variables from per-target config:
	while [ -z "$rev" ] && try_target_config; do :; done
	[ -z "$tree" ] && \
		err "$project/$target: tree unset" configure_target "$@"

	$if_build $if_not_dry_build $if_not_fetch \
		mk_dependencies -b $build_depend

	srcdir="src/$project/$tree"

	# specify directory containing e.g. coreboot defconfigs
	x_ mkdir -p "$xbtmp/noconfig/config"
	x_ touch "$xbtmp/noconfig/config/noconfig"
	[ "$noconfig" = "y" ] && \
		defscan="$xbtmp/noconfig/config" # no configs on this project
	( fx_ "eval exit 1 && err" find "$target_dir/config" -type f ) || \
		[ -n "$defscan" ] || defscan="$target_dir/config"

	# filename in srcdir when copying e.g. coreboot defconfig -> .config
	[ -z "$cfgname" ] && cfgname="$cfgname_default" # e.g. $srcdir/.config

	# if noconfig isn't set, there needs to be a config dir in xbmk.
	# coreboot needs a .config or it will behave unpredictably. we will
	# use the do_build macro to disable builds in this situation. this
	# does not disable mkhelpers or dry builds.
	[ ! -d "$target_dir/config" ] && [ "$noconfig" != "y" ] && \
		if_not_skip_build=":" # skip make on e.g. ./mk -b coreboot default

	[ "$XBMK_RELEASE" = "y" ] && [ "$release" = "n" ] && \
		return 1

	mdir="$xbmkpwd/config/submodule/$project/$tree"

	[ -f "CHANGELOG" ] && return 0 # disable fetching on release archives

	delete_old_project_files
	[ ! -f "CHANGELOG" ] && x_ fetch_trees; :
}

try_target_config()
{
	printf "Try %s/%s config: %s\n" "$project" "$_tcfg" "$target"
	eval "`newvar rev tree`"

	. "$_tcfg" || err "Can't read '$_tcfg'" configure_target "$@"
	if [ -n "$rev" ] && [ -z "$tree" ]; then
		# this makes setting tree= optional on configless projects
		tree="${_tcfg%/target.cfg}"
		tree="${tree##*/}"
	elif [ -z "$tree" ]; then
		err "$project/$target: tree unset" try_target_config "$@"
	fi

	[ "${_tcfg%/*/target.cfg}" = "${_tcfg%"/$tree/target.cfg"}" ] && \
		return 1 # target and tree matching was successful

	_ncfg="${_tcfg%/*/target.cfg}/$tree/target.cfg"
	[ "$_ncfg" = "$_tcfg" ] && \
		err "$project/$target: can't determine tree" "$@" # no change

	_tcfg="$_ncfg" # try next
}

# projects can specify which other projects
# to build first, as declared dependencies:
# TODO: detect infinite loops
mk_dependencies()
{
	bdf="${1-}"
	shift 1 2>/dev/null || :
	while [ $# -gt 0 ]; do
		bd_tree="${1##*/}"
		[ "${1%%/*}" = "$1" ] && bd_tree="" # build all trees

		[ -z "${1%%/*}" ] && \
			err "$project/$tree: !bd '$1'" mk_dependencies "$@"

		x_ "$mk" $bdf ${1%%/*} $bd_tree; :
		shift 1
	done; :
}

handle_buildcfg()
{
	[ ! -d "$srcdir" ] || for y in "$defscan"/*
	do
		buildcfg="$y"
		dest_dir="elf/$project/$tree/$target/${y##*/}"

		$if_build $if_not_dry_build \
			check_build_environment "$y" || continue

		$if_build \
			x_ $premake

		$if_not_skip_build \
			handle_makefile # also supports non-build operations

		$if_build \
			x_ $mkhelper; :

		$if_build $if_not_dry_build $if_not_skip_build \
			copy_elf; :
	done; :
}

check_build_environment()
{
	[ ! -f "$1" ] && return 1

	( fx_ "eval exit 1 && err" find "$dest_dir" -type f ) || return 1

	eval "$(printf '%s\n' "$xarch" | awk \
	    '{ for (i=1; i<=NF; i++) $i = "check_xgcc \""$i"\";"; print }')"
}

check_xgcc()
{
	cbdir="src/coreboot/$tree"

	[ "$project" != "coreboot" ] && \
		cbdir="src/coreboot/default"
	[ -n "$xgcctree" ] && \
		cbdir="src/coreboot/$xgcctree"

	cbelfdir="elf${cbdir#src}"

	xfix="${1%-*}"
	[ "$xfix" = "x86_64" ] && xfix="x64"

	xgccfile="$cbelfdir/xgcc_${xfix}_was_compiled"
	xgccargs="crossgcc-$xfix UPDATED_SUBMODULES=1 CPUS=$XBMK_THREADS"

	x_ "$mk" -f coreboot "${cbdir#src/coreboot/}"
	x_ mkdir -p "$cbelfdir"

	# tell build systems what cross-compiler to use
	export PATH="$xbmkpwd/$cbdir/util/crossgcc/xgcc/bin:$PATH"
	export CROSS_COMPILE="${xarch% *}-" # used by e.g. u-boot

	# coreboot-specific (we disable gnat on some trees)
	[ -n "$xlang" ] && \
		export BUILD_LANGUAGES="$xlang"

	[ -f "$xgccfile" ] && return 0 # skip (build already exists)

	make -C "$cbdir" $xgccargs || x_ make -C "$cbdir" $xgccargs

	x_ touch "$xgccfile" # prevent unnecessary re-build operations
}

handle_makefile()
{
	clean_src

	[ -f "$buildcfg" ] && \
		x_ cp "$buildcfg" "$srcdir/$cfgname"

	$if_not_skip_build $if_not_dry_build \
		run_make_command

	_copy="$cfgname"
	[ "$mode" = "savedefconfig" ] && \
		_copy="defconfig"

	$if_make_config $if_not_skip_build \
		x_ cp "$srcdir/$_copy" "$buildcfg"; :
}

run_make_command()
{
	[ -z "$cmakedir" ] || \
		x_ cmake -B "$srcdir" "$srcdir/$cmakedir"
	$if_build \
		check_autoconf "$srcdir"

	check_makefile "$srcdir" && \
	    x_ make -C "$srcdir" $mode -j$XBMK_THREADS $makeargs
}

check_autoconf()
{
	(
	x_ cd "$1"

	[ -f "bootstrap" ] && \
		x_ ./bootstrap $bootstrapargs
	[ -f "autogen.sh" ] && \
		x_ ./autogen.sh $autogenargs
	[ -f "configure" ] && \
		x_ ./configure $configureargs; :

	) || err "can't bootstrap project: $1" check_autoconf "$@"; :
}

check_makefile()
{
	[ ! -f "$1/Makefile" ] && [ ! -f "$1/makefile" ] && \
	    [ ! -f "$1/GNUmakefile" ] && return 1; :
}

copy_elf()
{
	[ -f "$listfile" ] && \
		x_ mkdir -p "$dest_dir"

	if [ -f "$listfile" ]; then
		while read -r f; do
			[ -f "$srcdir/$f" ] && \
				x_ cp "$srcdir/$f" "$dest_dir"; :

		done < "$listfile" || err \
		    "cannot read '$listfile'" "copy_elf" "$@"; :
	fi

	clean_src
}

clean_src()
{
	check_makefile "$srcdir" || return 0
	$if_dry_build return 0

	make -C "$srcdir" distclean $cleanargs || \
	    x_ make -C "$srcdir" clean $cleanargs
}

x_ main "$_f" "$@"

) || exit 1; :