summaryrefslogtreecommitdiffhomepage
path: root/subr/build_init.subr
blob: f5894e698cae6a4c6e80b033d6e92a89e17fae37 (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
#
# set +o errexit -o noglob -o nounset is assumed.
#

buildp_expand_restart_at() {
	local _restart_at_spec="${1}" _restart_at="" _restart_at_spec_new="";

	for _restart_at in $(rtl_llift "${_restart_at_spec}" "," " "); do
		case "${_restart_at}" in
		@fetch)	_restart_at="fetch_download,fetch_extract"; ;;
		@configure)
			_restart_at="configure_patch_pre,configure_autotools,configure_patch,configure"; ;;
		@build)	_restart_at="build"; ;;
		@install)
			_restart_at="install_subdirs,install_make,install_files,install_libs,install,install_rpm"; ;;
		@clean)
			_restart_at="clean"; ;;
		*)	;;
		esac;
		_restart_at_spec_new="$(rtl_lconcat "${_restart_at_spec_new}" "${_restart_at}" ",")"
	done;
	printf "%s" "${_restart_at_spec_new}";
};

buildp_expand_restart_recursive() {
	local _restart_spec="${1}" _last_pkg="" _restart_spec_at="" _restart_spec_recursive=0;

	case "${_restart_spec}" in
	\*\*\*[a-zA-Z]*)
			_restart_spec="${_restart_spec#\*\*\*}"; _restart_spec_recursive=3; ;;
	\*\*[a-zA-Z]*)	_restart_spec="${_restart_spec#\*\*}"; _restart_spec_recursive=2; ;;
	\*[a-zA-Z]*)	_restart_spec="${_restart_spec#\*}"; _restart_spec_recursive=1; ;;
	ALL)		_restart_spec_at=ALL; _restart_spec_recursive=2; ;;
	LAST)		_restart_spec_at=ALL; _restart_spec_recursive=0;
			if [ -n "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}" ]\
			&& [ -e "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}" ]; then
				_last_pkg="$(cat "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}")";
				rtl_fileop rm "${DEFAULT_BUILD_LAST_FAILED_PKG_FNAME}";
				rtl_state_clear "${BUILD_WORKDIR}" "${_last_pkg}";
				_restart_spec="${_last_pkg}";
			else
				_status="cannot rebuild last failed package"; return 1;
			fi; ;;
	esac;
	ARG_RESTART="${_restart_spec}";
	ARG_RESTART_AT="${_restart_spec_at}";
	ARG_RESTART_RECURSIVE="${_restart_spec_recursive}";
	return 0;
};

buildp_process_restart_spec() {
	local _restart_spec="${1}" _eqfl=0 _ltfl=0 _restart_spec_at0="" _restart_spec_at="" _step="" _step1=""; _status="";

	buildp_expand_restart_recursive "${_restart_spec}" || return "${?}";
	_restart_spec="${ARG_RESTART}"; _restart_spec_at="${ARG_RESTART_AT}";

	case "${_restart_spec}" in
	"")	return 0; ;;
	*:*)
		_restart_spec_at="${_restart_spec#*:}"; _restart_spec="${_restart_spec%%:*}";
		_restart_spec_at0="${_restart_spec_at}"; _restart_spec_at="";
		case "${_restart_spec_at0}" in
		ALL|LAST)
			_restart_spec_at="${_restart_spec_at0}"; ;;

		^*)	_restart_spec_at0="${_restart_spec_at0#^}";
			_restart_spec_at0="$(buildp_expand_restart_at "${_restart_spec_at0}")";
			_restart_spec_at="$(rtl_llift "${DEFAULT_BUILD_STEPS}" " 	" ",")";
			for _restart_at in $(rtl_llift "${_restart_spec_at0}" "," " "); do
				_restart_spec_at="$(rtl_lfilter "${_restart_spec_at}" "${_restart_at}" ",")";
			done; _restart_spec_at="$(rtl_lfilter "${_restart_spec_at}" "finish" ",")"; ;;

		\<=*|\<*|\>=*|\>*)
			[ "${_restart_spec_at0#<}" != "${_restart_spec_at0}" ] && _ltfl=1;
			if [ "${_restart_spec_at0#[<>]=}" != "${_restart_spec_at0}" ]; then
				_restart_spec_at0="${_restart_spec_at0#[<>]=}"; _eqfl=1;
			else
				_restart_spec_at0="${_restart_spec_at0#[<>]}"; _eqfl=0;
			fi; _restart_spec_at="";
			_restart_spec_at0="$(buildp_expand_restart_at "${_restart_spec_at0%%,*}")";
			if [ \( "${_eqfl}" -eq 1 \) -a \( "${_ltfl}" -eq 1 \) ]\
			|| [ \( "${_eqfl}" -eq 0 \) -a \( "${_ltfl}" -eq 0 \) ]; then
				_restart_spec_at0="${_restart_spec_at0##*,}";
			elif [ \( "${_eqfl}" -eq 1 \) -a \( "${_ltfl}" -eq 0 \) ]\
			||   [ \( "${_eqfl}" -eq 0 \) -a \( "${_ltfl}" -eq 1 \) ]; then
				_restart_spec_at0="${_restart_spec_at0%%,*}";
			fi;
			for _restart_at in ${DEFAULT_BUILD_STEPS}; do
				if [ "${_ltfl}" -eq 1 ]; then
					if [ "${_restart_at}" = "${_restart_spec_at0%%,*}" ]; then
						if [ "${_eqfl}" -eq 1 ]; then
							_restart_spec_at="$(rtl_lconcat "${_restart_spec_at}" "${_restart_at}" ",")";
						fi; break;
					fi;
				else
					if [ "${_restart_at}" = "${_restart_spec_at0%%,*}" ]; then
						_foundfl=1; [ "${_eqfl}" -eq 0 ] && continue;
					fi; [ "${_foundfl}" -eq 0 ] && continue;
				fi;
				_restart_spec_at="$(rtl_lconcat "${_restart_spec_at}" "${_restart_at}" ",")";
			done; ;;

		*)	_restart_spec_at="$(buildp_expand_restart_at "${_restart_spec_at0}")"; ;;
		esac; ;;

	*)	_restart_spec_at=ALL; ;;
	esac;

	ARG_RESTART="$(rtl_llift "${_restart_spec}" "," " ")";
	ARG_RESTART_AT="${_restart_spec_at}";
	if [ "${#ARG_RESTART_AT}" -eq 0 ]; then
		_status="zero-length build step list"; return 1;
	elif [ "${ARG_RESTART_AT}" != "ALL" ]\
	&&   [ "${ARG_RESTART_AT}" != "LAST" ]; then
		for _restart_at in $(rtl_lfilter "$(rtl_llift "${ARG_RESTART_AT}" "," " ")" "${DEFAULT_BUILD_STEPS}"); do
			case "${_restart_at}" in
			start)	;;
			*)	_status="unknown build step \`${_restart_at}'"; return 1; ;;
			esac;
		done;
	fi;
	if [ "${#ARG_RESTART_AT}" -gt 0 ]\
	&& [ "${ARG_RESTART_AT}" != "ALL" ]\
	&& [ "${ARG_RESTART_AT}" != "LAST" ]; then
		_restart_spec_at="${ARG_RESTART_AT}"; ARG_RESTART_AT="";
		for _restart_at in ${DEFAULT_BUILD_STEPS}; do
			if rtl_lmatch "${_restart_spec_at}" "${_restart_at}" ","; then
				ARG_RESTART_AT="$(rtl_lconcat "${ARG_RESTART_AT}" "${_restart_at}" ",")";
			fi;
		done;
		if [ "${ARG_RESTART_AT##*,}" != "finish" ]; then
			_step="$(rtl_lfilter "${ARG_RESTART_AT}" "clean,finish" ",")"; _step="${_step##*,}";
			_step1="$(rtl_lfilter "${DEFAULT_BUILD_STEPS}" "clean finish")"; _step1="${_step1##* }";
			if [ "${_step}" = "${_step1}" ]; then
				ARG_RESTART_AT="$(rtl_lconcat "${ARG_RESTART_AT}" "finish" ",")";
			fi;
		fi;
	fi;
	return 0;
};

buildp_init_args() {
	local	_eqfl=0 _foundfl=0 _group="" _pkg_names_unknown="" _rc=0	\
		EX_PKG_BUILD_GROUPS EX_PKG_BUILD_GROUPS_NOAUTO; _status="";

	case "${ARG_FETCH_FORCE}" in
	ipv4)	DEFAULT_GIT_ARGS="$(rtl_lconcat "-4" "${DEFAULT_GIT_ARGS}")";
		DEFAULT_WGET_ARGS="$(rtl_lconcat "-4" "${DEFAULT_WGET_ARGS}")"; ;;
	ipv6)	DEFAULT_GIT_ARGS="$(rtl_lconcat "-6" "${DEFAULT_GIT_ARGS}")";
		DEFAULT_WGET_ARGS="$(rtl_lconcat "-6" "${DEFAULT_WGET_ARGS}")"; ;;
	esac;
	if [ -z "${BUILD_HNAME:-}" ]\
	&& ! BUILD_HNAME="$(hostname)"; then
		_rc=1; _status="Error: failed to obtain hostname.";
	elif [ "${ARG_DUMP_ON_ABORT:-0}" -eq 1 ]\
	&& [ "${ARG_RELAXED:-0}" -eq 1 ]; then
		_rc=1; _status="Error: --dump-on-abort excludes -R.";
	elif [ "${ARG_AS_NEEDED:-0}" -eq 1 ]\
	&& [ -e "${PREFIX}/build.gitref" ]\
	&& [ "$(git rev-parse HEAD)" = "$(cat "${PREFIX}/build.gitref")" ]; then
		_rc=0; _status="Git repository has not changed since last build and --as-needed was specified.";
	elif ! buildp_process_restart_spec "${ARG_RESTART:-}"; then
		_rc=1; _status="Error: failed to process -r specification: ${_status}.";
	elif ! ex_pkg_load_groups; then
		_rc=1; _status="Error: failed to load build groups.";
	else	if ! rtl_lmatch "${ARG_DIST:-}" "rpm" ","\
		&& [ -z "${ARG_DUMP_IN:-}" ]\
		&& [ "${ARG_DUMP_ON_ABORT:-0}" -eq 0 ]; then
			EX_PKG_BUILD_GROUPS="$(rtl_lfilter "${EX_PKG_BUILD_GROUPS}" "host_deps_rpm")";
		fi;
		if [ -z "${BUILD_GROUPS}" ]; then
			BUILD_GROUPS="${EX_PKG_BUILD_GROUPS}";
		else	_foundfl=0; for _group in ${BUILD_GROUPS}; do
				if rtl_lmatch "${EX_PKG_BUILD_GROUPS}" "${_group}"; then
					_foundfl=1; break;
				fi;
			done;
			if [ "${_foundfl}" -eq 0 ]; then
				_foundfl=0; for _group in ${BUILD_GROUPS}; do
					if rtl_lmatch "${EX_PKG_BUILD_GROUPS}" "${_group}"; then
						_rc=1; _status="Error: unknown build group \`${_group}'."; break;
					fi;
				done;
			fi;
		fi;
		if [ "${_rc:-0}" -eq 0 ]; then
			if [ -n "${ARG_DIST}" ]; then
				BUILD_GROUPS="$(rtl_lconcat "$(rtl_lfilter "${BUILD_GROUPS}" "dist")" "dist")";
			fi;
			if [ -n "${ARG_RESTART}" ] && ! rtl_lmatch "${ARG_RESTART}" "ALL LAST"; then
				for _pkg_name in ${ARG_RESTART}; do
					if ! ex_pkg_find_package "${BUILD_GROUPS}" "${_pkg_name}" >/dev/null; then
						_pkg_names_unknown="$(rtl_lconcat "${_pkg_names_unknown}" "${_pkg_name}")";
					fi;
				done;
				case "$(rtl_llength "${_pkg_names_unknown}")" in
				0)	;;
				1)	_rc=1; _status="Error: unknown package \`${_pkg_names_unknown}'."; ;;
				*)	_rc=1; _status="Error: unknown packages: $(rtl_subst "${_pkg_names_unknown}" " " ", ")"; ;;
				esac;
			fi;
		fi;
	fi; return "${_rc}";
};

buildp_init_env() {
	local _fname="" _rc=0; _status="";
	if ! cd "${0%/*}"; then
		printf "Error: failed to change working directory to \`${0%/*}'." >&2; exit 1;
	elif ! umask 022; then
		printf "Error: failed to set umask(2).\n" >&2; exit 1;
	elif ! BUILD_USER="$(id -nu)"; then
		printf "Error: failed to obtain username." >&2; exit 1;
	else	for _fname in $(find subr.rtl -name *.subr) $(find subr -name *.subr); do
			if ! . "${_fname}"; then
				printf "Error: failed to source \`%s'.\n" "${_fname}"; exit 1;
			fi;
		done;
	fi; return "${_rc}";
};

buildp_init_files() {
	local _log_last_fname="" _log_last_num=1 _rc=0; _status=""
	if ! rtl_fileop mkdir "${BUILD_DLCACHEDIR}" "${BUILD_WORKDIR}"\
	|| rtl_lmatch "${ARG_DIST}" "rpm" ","\
	&& ! rtl_fileop mkdir "${PREFIX_RPM}"; then
		_rc=1; _status="Error: cannot create build directories.";
	elif [ -e "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
		_rc=1; _status="Error: another build targeting this architecture and build type is currently in progress.";
	elif ! rtl_clean_env "${DEFAULT_CLEAR_ENV_VARS_EXCEPT}"; then
		_rc=1; _status="Error: failed to clean environment.";
	elif ! rtl_check_path_vars "${DEFAULT_CHECK_PATH_VARS}"; then
		_rc=1; _status="${_status}";
	else	export TMP="${BUILD_WORKDIR}" TMPDIR="${BUILD_WORKDIR}";
		touch "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}";
		if [ -e "${DEFAULT_BUILD_LOG_FNAME}" ]; then
			while [ -e "${DEFAULT_BUILD_LOG_FNAME}.${_log_last_num}" ]; do
				: $((_log_last_num+=1));
			done;
			_log_last_fname="${DEFAULT_BUILD_LOG_FNAME}.${_log_last_num}";
			rtl_fileop mv "${DEFAULT_BUILD_LOG_FNAME}" "${_log_last_fname}";
			rtl_fileop ln_symbolic "${_log_last_fname}" "${DEFAULT_BUILD_LOG_LAST_FNAME}";
		fi;
		rtl_fileop touch "${DEFAULT_BUILD_LOG_FNAME}"; rtl_log_set_fname "${DEFAULT_BUILD_LOG_FNAME}";
		if rtl_lmatch "${ARG_CLEAN_BUILDS}" "prefix" ","; then
			trap "rm -f \"${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}\" 2>/dev/null; rtl_log_msg fatalexit \"Build aborted.\"" HUP INT TERM USR1 USR2;
			rtl_log_msg info "-C prefix specified, cleaning prefix...";
			for _pname in ${DEFAULT_CLEAR_PREFIX_PATHS}; do
				if ! rtl_fileop rm "${PREFIX}/${_pname}"; then
					_rc=1; _status="Error: failed to remove \`${PREFIX}/${_pname}'."; break;
				fi;
			done;
			trap - HUP INT TERM USR1 USR2;
		fi;
		export PATH="${PREFIX}/bin${PATH:+:${PATH}}";
	fi;
	return "${_rc}";
};

buildp_init_getopts() {
	local _arg="" _opt="" _rc=0 _shiftfl=0 OPTIND=0; _status="";
	: ${ARCH:="nt64"}; : ${BUILD_KIND:="debug"};
	ARG_AS_NEEDED=0; ARG_CLEAN_BUILDS=""; ARG_DEBUG_MINIPIX=0; ARG_DIST=""; ARG_DUMP_IN="";
	ARG_DUMP_ON_ABORT=0; ARG_FETCH_FORCE=""; ARG_PARALLEL=1; ARG_RELAXED=0; ARG_RESET_PKG=0;
	ARG_RESTART=""; ARG_RESTART_AT=""; ARG_RESTART_RECURSIVE=""; ARG_VERBOSE=0;
	while [ "${#}" -gt 0 ]; do
		case "${1}" in
		--as-needed)	ARG_AS_NEEDED=1; _shiftfl=1; ;;
		--dump-in)	if [ "${#}" -ge 2 ]; then
					ARG_DUMP_IN="${2}"; ARG_DUMP_ON_ABORT=1; _shiftfl=2;
				else
					_rc=1; _status="Error: missing argument to option --dump-in.";
				fi; ;;
		--dump-on-abort)
				ARG_DUMP_ON_ABORT=1; _shiftfl=1; ;;
		--debug-minipx)	ARG_DEBUG_MINIPIX=1; _shiftfl=1; ;;
		--help)		cat etc/build.usage; exit 0; ;;
		--reset-state)	ARG_RESET_PKG=1; _shiftfl=1; ;;
		-v*)		_opt="${1#-}"; while [ -n "${_opt}" ]; do
					: $((ARG_VERBOSE+=1)); _opt="${_opt#?}";
				done; _shiftfl=1; ;;
		# {{{ --roar
		--roar)		printf "%s\n" '
                                 
                                 
%.           /\       /\    ROAR!
`%%.             """       /     
 `%%            o _ o     /      
 //        ;   (__Y__)           
((        /     `\_/             
 \\     .'\''                       
  \\   /                         
   \\/    \    | |               
    \      )___| |               
   (_____/__)))))))              '; exit 0; ;;
		# }}}
		*)		_shiftfl=0; ;;
		esac;
		if [ "${_rc}" -ne 0 ]; then
			break;
		elif [ "${_shiftfl}" -gt 0 ]; then
			shift "${_shiftfl}"; continue;
		elif getopts a:b:C:D:F:hp:Pr:R _opt; then
			case "${_opt}" in
			a)	ARCH="${OPTARG}"; ;;
			b)	BUILD_KIND="${OPTARG}"; ;;
			C)	ARG_CLEAN_BUILDS="${OPTARG}"; ;;
			D)	ARG_DIST="${OPTARG}"; ;;
			F)	ARG_FETCH_FORCE="${OPTARG}"; ;;
			h)	cat etc/build.usage.short; exit 0; ;;
			p)	ARG_PARALLEL="${OPTARG}"; ;;
			P)	ARG_PARALLEL="auto";
				if [ -n "${2:-}" ]\
				&& rtl_isnumber "${2}"; then
					_rc=1; _status="Error: maximum parallelisation job count is set with the \`-p jobs' option."; break
				fi; ;;
			r)	ARG_RESTART="${OPTARG}"; ;;
			R)	ARG_RELAXED=1; ;;
			*)	cat etc/build.usage.short; exit 1; ;;
			esac; shift $((${OPTIND}-1)); OPTIND=1;
		else	if rtl_match "${1}" "=*"; then
				BUILD_GROUPS_INHIBIT_DEPS=1; _arg="${1#=}";
			else
				_arg="${1}";
			fi;
			case "${_arg}" in
			*=*)		rtl_set_var_unsafe "${_arg%%=*}" "${_arg#*=}"; ;;
			[^a-zA-Z]*)	_rc=1; _status="Error: build group names must start with [a-zA-Z] (in argument \`${_arg}'.)"; ;;
			*[^_a-zA-Z]*)	_rc=1; _status="Error: build group names must not contain [^_a-zA-Z] (in argument \`${_arg}'.)"; ;;
			*)		BUILD_GROUPS="$(rtl_lconcat "${BUILD_GROUPS}" "${_arg}")"; ;;
			esac; shift;
		fi;
	done;
	if [ "${_rc:-0}" -eq 0 ]; then
		case "${ARG_PARALLEL}" in
		auto)	if ! ARG_PARALLEL="$(rtl_get_cpu_count)"; then
				_rc=1; _status="Error: failed to get CPU count.";
			else
				ARG_PARALLEL=$((${ARG_PARALLEL}/2));
			fi; ;;
		"")	ARG_PARALLEL=1; ;;
		*)	if ! rtl_isnumber "${ARG_PARALLEL}"; then
				_rc=1; _status="Error: invalid jobs count \`${ARG_PARALLEL}'.";
			fi; ;;
		esac;
		if [ "${_rc:-0}" -eq 0 ]; then
			DEFAULT_BUILD_CPUS="${ARG_PARALLEL}";
			case "${ARG_VERBOSE:-0}" in
			0)	rtl_fileop_set_log 0; rtl_log_set_lvl 0; ;;
			1)	rtl_fileop_set_log 0; rtl_log_set_lvl 2; ;;
			2)	rtl_fileop_set_log 0; rtl_log_set_lvl 2; ;;
			3)	rtl_fileop_set_log 0; rtl_log_set_lvl 2; ;;
			4)	rtl_fileop_set_log 1; rtl_log_set_lvl 3; ;;
			*)	_rc=1; _status="Error: invalid verbosity level (max. -vvvv)"; ;;
			esac;
		fi;
	fi;
	return "${_rc}";
};

buildp_init_prereqs() {
	if ! rtl_check_prereqs \
			awk bunzip2 bzip2 cat chmod cmake cp date find flock	\
			g++ gcc git grep gunzip gzip hostname id install kill	\
			ln lzip make mkdir mkfifo mktemp mv paste patch perl	\
			pgrep pkill printf readlink rm sed sha256sum sort	\
			stat tail tar test touch tr uniq wget xz zip; then
		printf "%s\n" "${_status}" >&2; exit 1;
	elif ! awk -V 2>/dev/null | grep -q "^GNU Awk "; then
		printf "Error: awk(1) in \$PATH must be GNU Awk." >&2; exit 1;
	elif ! (FNAME="$(mktemp)" && { trap "rm -f \"\${FNAME}\"" EXIT;		\
			sed -i'' -e '' "${FNAME}" >/dev/null 2>&1; }); then
		printf "Error: sed(1) in \${PATH} does not support the \`-i' option.\n" >&2; exit 1;
	fi;
};

build_init() {
	local _rc=0; _status="";
	if ! buildp_init_env			\
	|| ! buildp_init_getopts "${@}"		\
	|| ! buildp_init_prereqs		\
	|| ! ex_pkg_load_vars			\
	|| ! buildp_init_args			\
	|| ! buildp_init_files; then
		_rc=1; _status="${_status}";
	fi; return "${_rc}";
};

# vim:filetype=sh