aboutsummaryrefslogtreecommitdiff
path: root/build
blob: 5ff703180c969d01b86cf23557a005d2949d0223 (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
#!/bin/sh
set -e

## true
# avoid the shell overhead of fork/exec'ing just to call /bin/true
# returns true
true() { : ; }

## false
# avoid the shell overhead of fork/exec'ing just to call /bin/false
# returns false
false() { ! : ; }

## error <message>
# displays the supplied <message> on stderr
error() { echo "error: $*" >&2; }

## die <message>
# display the supplied <message> and exit with an error
die() { error "$*"; exit 1; }

## simple_usage <cmd> <summary>
# A simplified usage interface for commands with no option arguments.
simple_usage()
{
	cmd="${1}"
	summary="${2}"
	shift 2

	if test "$#" -eq '0'; then
		error "no arguments to command"
		echo "try 'build ${cmd} --help'" >&2
		exit 1
	fi

	while test "$#" -gt '0'; do
		case "${1}" in
		(-h|-help|--help)
			echo "usage: build ${cmd} ${summary}" 2>&1
			exit 0;;
		(--)	shift; break;;
		(-*)	error "unknown option '${1}'"
			echo "try 'build ${cmd} --help'" >&2
			exit 1;;
		(*)	break;;
		esac
	done
}

## mkenv
# prepare the environment structure for the current package
mkenv()
{
	for dir in "${W}" "${L}" "${E}" "${D}" "${T}"; do
		if [ ! -d "${dir}" ]; then
			mkdir -p "${dir}"
		fi
	done
	unset dir

	if [ "$#" -gt "0" ]; then
		cat /dev/null > "${L}/${1}.log"
		set > "${E}/${1}.env"
	fi
}

## import
# import a builder library.
# During import a library can check 'BUILDER_CALL_STACK' to see if it equals
# '__main__'.  If it does, then the library is being executed directly.
# Further more, builder-libraries can do load-time tests in order to perform
# various one-time optimizations.
#
# Note: this is borrowed from 'shlib' almost verbatim w/ the only change being
# the variable names.
import()
{
	set -- "${1}" "`echo "${1}" | tr '-' '_'`"
	test -f "${BUILDER_LIBDIR}/${1}" || die "library does not exist '${1}'"
	eval "test -z \"\${BUILDER_IMPORT_${2}}\"" || return
	BUILDER_CALL_STASH="${BUILDER_CALL_STACK}"
	BUILDER_CALL_STACK="${BUILDER_CALL_STACK} ${1}" . "${BUILDER_CALL_LIBDIR}/${1}"
	eval "BUILDER_IMPORT_${2}='${BUILDER_LIBDIR}/${1}'"
	if [ -z "${BUILDER_IMPORTED}" ]; then
		BUILDER_IMPORTED="${1}"
	else
		BUILDER_IMPORTED="${BUILDER_IMPORTED} ${1}"
	fi
	BUILDER_CALL_STACK="${BUILDER_CALL_STASH}"
}

## include
# include a builder config.
# This command simply sources the named config found in TOPDIR/.builder/
include()
{
	set -- "${1}" "`echo "${1}" | tr '-' '_'`"
	test -f "${BUILDER_CFGDIR}/${1}" || die "config not found '${1}'"
	eval "test -z \"\${BUILDER_INCLUDE_${2}}\"" || return
	. "${BUILDER_CFGDIR}/${1}"
	eval "BUILDER_INCLUDE_${2}='${BUILDER_CFGDIR}/${1}'"
	if [ -z "${BUILDER_INCLUDED}" ]; then
		BUILDER_INCLUDED="${1}"
	else
		BUILDER_INCLUDED="${BUILDER_INCLUDED} ${1}"
	fi
}

# FIXME we need a build-resolv subcmd for resolving package names
parse_pkg_name()
{
	# Only allow a single '/' in the name
	if [ "1${1##*/}" != "1${1#*/}" ]; then
		return 1
	fi

	if [ "2${1#*/}" != "2${1}" ]; then
		printf '%s' "${1}"
	else
		printf '%s' "${1}/all"
	fi
}

parse_name()
{
	if ! parse_pkg_name "${1}" > /dev/null 2>&1; then
		return "$?"
	fi

	set -- "$(parse_pkg_name "${1}")"
	printf '%s' "${1#*/}"
}

parse_category()
{
	if ! parse_pkg_name "${1}" > /dev/null 2>&1; then
		return "$?"
	fi

	set -- "$(parse_pkg_name "${1}")"
	printf '%s' "${1%/*}"
}

## load_rules <package>
# load the Buildrules of a package into the current program space
load_rules()
{
	# We can aquire this data before we bother loading anything
	NAME="$(parse_name "${1}")"
	CATEGORY="$(parse_category "${1}")"
	PKG_NAME="${CATEGORY}/${NAME}"

	if [ ! -d "${BUILDER_PKGDIR}/${PKG_NAME}" ]; then
		die "no such package '${1}'"
	fi

	RULESFILE="${BUILDER_PKGDIR}/${CATEGORY}/${NAME}/Buildrules"
	if [ ! -f "${RULESFILE}" ]; then
		die "no rulesfile for package '${1}'"
	fi

	export NAME CATEGORY PKG_NAME RULESFILE

	# These variables are used by the Buildrules and fundmentally make up
	# the majority of their environ data
	F="${BUILDER_PKGDIR}/${CATEGORY}/${NAME}/files"
	W="${BUILDER_TMPDIR}/${CATEGORY}/${NAME}/work"
	L="${BUILDER_TMPDIR}/${CATEGORY}/${NAME}/log"
	E="${BUILDER_TMPDIR}/${CATEGORY}/${NAME}/env"
	T="${BUILDER_TMPDIR}/${CATEGORY}/${NAME}/tmp"
	D="${BUILDER_TMPDIR}/${CATEGORY}/${NAME}/install"

	export F W L E T D

	# Source in the category Buildrules
	if [ -f "${BUILDER_PKGDIR}/${CATEGORY}/.buildrules" ]; then
		. "${BUILDER_PKGDIR}/${CATEGORY}/.buildrules"
	fi

	# These variables are only set within a Rulesfile and thus need to be
	# cleared before sourcing it in.
	VERSION=
	RELEASE=
	DESCRIPTION=
	LICENSE=
	SOURCE_URI=
	PATCHES=
	RDEPENDS=
	BDEPENDS=
	EXTRA_SOURCES=

	# Source in the package Buildrules
	. "${RULESFILE}"

	if [ "${CATEGORY}/${NAME}" != "${1}" ]; then
		die "Buildrules can not set the package name"
	fi

	if [ -z "${VERSION}" ]; then
		die "missing version in '${NAME}'"
	fi

	if [ -z "${DESCRIPTION}" ]; then
		die "missing description in '${NAME}'"
	fi

	if test -z "${RELEASE}"; then
		RELEASE="${VERSION#*-}"
		if ! test -z "${RELEASE}" && test "${RELEASE}" != "${VERSION}"; then
			VERSION="${VERSION%-${RELEASE}}"
			if test -z "${S}"; then
				S="${W}/${NAME}-${VERSION}-${RELEASE}"
			fi
		else
			RELEASE='0'
		fi
	fi
	if test -z "${S}"; then
		S="${W}/${NAME}-${VERSION}"
	fi

	export VERSION RELEASE DESCRIPTION LICENSE SOURCE_URI PATCHES BDEPENDS RDEPENDS
}

if [ "${BUILDER_DEBUG:-0}" != '0' ]; then
	set -x
fi

##
# Check to see if we are wrapping a sub-command
BUILDER_COMMAND=
if [ -f "${1}" ]; then
	case "${1##*/}" in
	(build-*)
		BUILDER_COMMAND="${1}"
		shift
		. "${BUILDER_COMMAND}"
		# exit with the exit status of the last command from within the
		# sub-script.  This is normal shell behavior, we are just
		# making it explicit.
		exit $?
		;;
	esac
fi

##
# We are being called as 'build [options] <command>'.  Setup the environment
# for execution of a sub-command.

BUILDER_DEBUG=0
while [ "$#" -gt "0" ]; do
	case "$1" in
	(-v|-version|--version)	version; exit 0;;
	(-d|-debug|--debug)
		BUILDER_DEBUG=1
		set -x
		;;
	(-h|-help|--help)
		shift 1
		set -- help "$@"
		break
		;;
	(-*)	echo "error: unknown option '${1}'" >&2
		echo "try '${0} help'" >&2
		exit 1
		;;
	(*)	break
		;;
	esac
	shift 1
done
export BUILDER_DEBUG

##
# Done with arguments, time for setting up the enviroment for the build
TOPDIR="${PWD}"
while [ ! -z "${TOPDIR}" ]; do
	[ -d "${TOPDIR}/.builder" ] && break
	TOPDIR="${TOPDIR%/*}"
done
if [ -z "${TOPDIR}" ]; then
	echo "error: current path not in a build-tree" >&2
	exit 1
fi

# We are being executed as 'build <cmd>'
BUILDER_CALL_STACK='__main__'
export BUILDER_CALL_STACK

# set the builtin defaults based on TOPDIR.  We export TOPDIR as BUILDER_TOPDIR
# to avoid stepping on the potential usage of TOPDIR within package Makefiles
BUILDER_TOPDIR="${TOPDIR}"
BUILDER_CFGDIR="${TOPDIR}/.builder"
BUILDER_PKGDIR="${TOPDIR}/packages"
BUILDER_SRCDIR="${TOPDIR}/sources"
BUILDER_ATFDIR="${TOPDIR}/artifacts"
BUILDER_LIBDIR="${TOPDIR}/scripts/libexec"
BUILDER_TMPDIR="${TOPDIR}/tmp"

export BUILDER_CFGDIR BUILDER_PKGDIR BUILDER_SRCDIR
export BUILDER_ATFDIR BUILDER_LIBDIR BUILDER_TOPDIR
export BUILDER_TMPDIR

# The default SYSROOT .. Much of the toolchain already obeys SYSROOT .. we
# aren't setting a policy so much as obeying one.
SYSROOT="${SYSROOT:-${BUILDER_TOPDIR}/sysroot}"
export SYSROOT

# We save the pre-config PATH as BUILDER_PATH to be used by downstream tools.
# FIXME detect prefixing BUILDER_LIBDIR onto PATH multiple times?
BUILDER_PATH="${BUILDER_LIBDIR}:${PATH}"
PATH="${BUILDER_PATH}"
export BUILDER_PATH PATH

# include our default config
test -f "${BUILDER_CFGDIR}/config" && include config

# FIXME MAKE_OPTS should default to #CPUS + 1, until we can detect #CPUS, we
# just set it to 2 for uni-processor systems.
MAKE_OPTS="${MAKE_OPTS:--j2}"
export MAKE_OPTS

# Default build target
# FIXME this should default to all/all
TARGET="${TARGET:-all/all}"
export TARGET

# If unspecified go ahead and ask gcc
CBUILD="${CBUILD:-$(build-dumpmachine)}"
export CBUILD

ARCHIVE_FORMAT="${ARCHIVE_FORMAT:-tar.bz2}"
export  ARCHIVE_FORMAT

# FIXME this stuff needs to be detected in a more reliable fashion
ARCH="${ARCH:-$(uname -m)}"
export ARCH

CHOST="${CHOST:-${CBUILD}}"
export CHOST

# if we aren't given an action then we do everything
ACTION="install"
if [ "$#" -gt "0" ]; then
	ACTION="${1}"
	shift 1
fi

if command -v "build-make-${ACTION}" 2>&1 > /dev/null; then
	BUILDER_COMMAND="$(command -v 'build-make')"
	set -- "${ACTION}" "${@}"
elif command -v "build-${ACTION}" 2>&1 > /dev/null; then
	BUILDER_COMMAND="$(command -v "build-${ACTION}")"
else
	error "unknown action '${ACTION}'"
	echo "try '${0} help'" >&2
	exit 1
fi
export BUILDER_COMMAND

exec "${BUILDER_COMMAND}" "${@}"