summaryrefslogtreecommitdiffhomepage
path: root/subr/build_init.subr
blob: ee70bc2fed8df93f3d76df21b44a7a9c312097b2 (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
build_ast() {
	local _param="${1}" _pids_killed="";
	rm -f "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}";
	case "${_param}" in
	abort) ex_rtl_log_msg failexit "Build aborted."; ;;
	esac;
	ex_rtl_kill_tree "${$}";
	if [ -n "${_pids_killed}" ]; then
		ex_rtl_log_msg vnfo "Killed PIDs ${_pids_killed}";
	fi;
};

build_fini() {
	: $((BUILD_TIMES_SECS=$(ex_rtl_date %s)-${BUILD_TIMES_SECS}));
	: $((BUILD_TIMES_HOURS=${BUILD_TIMES_SECS}/3600));
	: $((BUILD_TIMES_MINUTES=(${BUILD_TIMES_SECS}%3600)/60));
	: $((BUILD_TIMES_SECS=(${BUILD_TIMES_SECS}%3600)%60));
	if [ -f "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
		ex_rtl_fileop rm ${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME};
	fi;
};

build_init() {
	local __="" _env_vars="" _env_vars_except="" _env_var="" _log_last_fname="" _log_last_ts="" _vname="" _vname_val="";
	umask 022;
	if ! awk -V 2>/dev/null | grep -q "^GNU Awk "; then
		ex_rtl_log_msg failexit "Error: awk(1) in \$PATH must be GNU Awk.";
	fi;
	if ! which pgrep >/dev/null 2>&1; then
		ex_rtl_log_msg failexit "Error: missing pgrep.";
	fi;
	if [ -z "${DEFAULT_BUILD_CPUS}" ]\
	&& [ -e /proc/cpuinfo ]; then
		DEFAULT_BUILD_CPUS=$(awk '/^processor/{cpus++} END{print cpus}' /proc/cpuinfo);
	fi;
	if [ "${ARG_PARALLEL}" = auto ]; then
		ARG_PARALLEL="${DEFAULT_BUILD_CPUS}";
	fi;
	: ${ARCH:=nt64}; : ${BUILD:=debug};
	if [ "${ARCH}" = nt32 ]; then
		DEFAULT_TARGET="i686-nt32-midipix";
	elif [ "${ARCH}" = nt64 ]; then
		DEFAULT_TARGET="x86_64-nt64-midipix";
	else
		if [ "${BUILD}" != debug ]\
		&& [ "${BUILD}" != release ]; then
			ex_rtl_log_msg failexit "Error: unknown build type \`${BUILD}'.";
		fi;
		ex_rtl_log_msg failexit "Error: invalid architecture \`${ARCH}'.";
	fi;
	for __ in ${HOME}/midipix_build.vars ${HOME}/.midipix_build.vars ../midipix_build.vars ./vars/env.vars; do
		[ -e ${__} ] && . ${__};
	done;
	for __ in $(find ./groups -name *.group | sort); do
		. "${__}"; __="${__##*/}"; __="${__%.group}";
		if [ "${__#[0-9][0-9][0-9].*}" != "${__}" ]\
		&& [ "${__#*[1-9].*}" = "${__}" ]\
		&& [ "${__}" != "999.invariants.group" ]; then
			GROUPS_DEFAULT="${GROUPS_DEFAULT:+${GROUPS_DEFAULT} }${__#*.}";
		fi;
	done;
	if [ "${ARG_AS_NEEDED:-0}" -eq 1 ]\
	&& [ -e "${PREFIX}/build.gitref" ]\
	&& [ "$(git rev-parse HEAD)" = "$(cat "${PREFIX}/build.gitref")" ]; then
		ex_rtl_log_msg info "Git repository has not changed since last build and --as-needed was specified.";
		exit 0;
	fi;
	export PATH="${PREFIX}/bin${PATH:+:${PATH}}";
	if [ "${ARG_FETCH_FORCE}" = "ipv4" ]; then
		DEFAULT_GIT_ARGS="-4${DEFAULT_GIT_ARGS:+ ${DEFAULT_GIT_ARGS}}";
		DEFAULT_WGET_ARGS="-4${DEFAULT_WGET_ARGS:+ ${DEFAULT_WGET_ARGS}}";
	elif [ "${ARG_FETCH_FORCE}" = "ipv6" ]; then
		DEFAULT_GIT_ARGS="-6${DEFAULT_GIT_ARGS:+ ${DEFAULT_GIT_ARGS}}";
		DEFAULT_WGET_ARGS="-6${DEFAULT_WGET_ARGS:+ ${DEFAULT_WGET_ARGS}}";
	fi;
	_env_vars="$(export | sed -ne '/^export/{s/^export //;s/=.*$//p}')";
	_env_vars_except="${DEFAULT_CLEAR_ENV_VARS_EXCEPT}";
	for _env_var in ${_env_vars}; do
		if [ "${_env_var#DEFAULT_}" != "${_env_var}" ]\
		|| [ "${_env_var#PKG_}" != "${_env_var}" ]; then
			_env_vars_except="${_env_vars_except:+${_env_vars_except} }${_env_var}";
		fi;
	done;
	ex_rtl_unset_vars $(ex_rtl_lfilter "${_env_vars}" "${_env_vars_except}");
	ex_rtl_fileop mkdir "${BUILD_DLCACHEDIR}" "${BUILD_WORKDIR}";
	if ex_rtl_lmatch "${ARG_DIST}" , rpm; then
		ex_rtl_fileop mkdir "${PREFIX_RPM}";
	fi;
	if [ -e "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}" ]; then
		ex_rtl_log_msg failexit "Error: another build targeting this architecture and build type is currently in progress.";
	else
		touch "${DEFAULT_BUILD_STATUS_IN_PROGRESS_FNAME}";
	fi;
	if [ -e "${DEFAULT_BUILD_LOG_FNAME}" ]; then
		_log_last_ts="$(stat -c %Y "${DEFAULT_BUILD_LOG_FNAME}")";
		_log_last_ts="$(awk 'BEGIN {printf(strftime("%Y-%m-%d-%H-%M-%S", '"${_log_last_ts}"'))}')";
		_log_last_fname="${DEFAULT_BUILD_LOG_FNAME}.${_log_last_ts}";
		ex_rtl_fileop mv "${DEFAULT_BUILD_LOG_FNAME}" "${_log_last_fname}";
		ex_rtl_fileop ln_symbolic "${_log_last_fname}" "${DEFAULT_BUILD_LOG_LAST_FNAME}";
	fi;
	ex_rtl_fileop touch "${DEFAULT_BUILD_LOG_FNAME}";
	BUILD_IS_PARENT=1;
	BUILD_DATE_START="$(ex_rtl_date %Y-%m-%d-%H-%M-%S)";
	BUILD_NFINI="${BUILD_NSKIP:=${BUILD_NFAIL:=${BUILD_NBUILT:=0}}}";
	BUILD_TIMES_SECS="$(ex_rtl_date %s)";
	BUILD_PKGS_FAILED="";
	trap "build_ast abort" HUP INT TERM USR1 USR2; trap "build_ast exit" EXIT;
	for _vname in ${DEFAULT_CHECK_PATH_VARS}; do
		_vname_val="$(ex_rtl_get_var_unsafe "${_vname}")";
		if [ -z "${_vname_val}" ]; then
			ex_rtl_log_msg failexit "Error: variable \`${_vname}' is empty or unset.";
		elif [ "${_vname_val#* *}" != "${_vname_val}" ]; then
			ex_rtl_log_msg failexit "Error: variable \`${_vname}' contains one or more whitespace characters.";
		fi;
	done;
	if ex_rtl_lmatch "${ARG_CLEAN_BUILDS}" , prefix; then
		ex_rtl_log_msg info "-C prefix specified, cleaning prefix...";
		for _pname in ${DEFAULT_CLEAR_PREFIX_PATHS}; do
			ex_rtl_fileop rm "${PREFIX}/${_pname}";
		done;
	fi;
};

# vim:filetype=sh