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

## usage
# Simply display the builder usage.  Though it would be nice if some of this
# information was pushed down into the sub-commands.
usage()
{
cat<<EOF
usage: ${0##*/} [options] <command> [command-opts] [all|<category>/<package|all> ...]

Options
-------
 -v, --version	Display the builder version.
 -d, --debug	Enable debug logging.
 -h, --help	Display the builder help and exit (may appear anywhere on the
		command line).

Commands
--------
  query		The query is used internally by builder, while allowing
		one to query various packages from the builder repository.

		options
		-------

		  -b, --build-deps
		  -d, --depends
		  -n, --name
		  -d, --description
		  -v, --version
		  -u, --source-uri
		  -s, --summary

  source	Create a copy/checkout of the package sources inside of
		packages/<category>/<name>/source.  The source/ directory
		within a package takes precendences over the SOURCE_URI,
		allowing in-place development of various packages.  This is
		particularly useful if the SOURCE_URI is an SCM.

  fetch		Fetch the sources for a package from the SOURCE_URI and store
		them into the sources/ top-level directory.  This is done
		automatically for all commands which depend on it.

  package	Prep, compile and construct a binary "artifact" file for a
		given package.  This command is performed automatically for all
		commands which depend on it.

  install	Install a binary artifact into the sysroot. This action is
		performed automatically for any packages which the current
		target depends on.  If necessary produce binary artifacts for
		all package deps.

  clean		Clean specified package from sysroot and artifacts.

  distclean	Clean up specified package from sysroot, artifacts, and sources.

EOF
}

## 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
}

## cleanup
# deal with temporary junk when we exit
cleanup()
{
	test -f "${BUILDER_MAKEFILE}" && rm -f "${BUILDER_MAKEFILE}"
	test -p "${BUILDER_PIPE}" && rm -f "${BUILDER_PIPE}"
}

## 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
}

## import <package>
# import a package into the current program space
import()
{
	# Evaluate our environment data
	# There is a bit of a performance penalty here in that the query
	# routine will source the build-rules to finish collecting data, so we
	# end up sourcing the Buildrules twice.
	eval $(build-query --environ "${1}")
	RELEASE=
	S=

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

	# 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
}

if [ ! -z "${BUILDER_DEBUG}" ]; then
	set -x
fi

##
# Check to see if we are wrapping a sub-command
# FIXME support custom commands
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

for arg in "$@"; do
	case "${arg}" in
	(-h|-help|--help)	usage; exit 0;;
	esac
done
unset arg

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

##
# 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

# 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_PKGDIR="${TOPDIR}/packages"
BUILDER_SRCDIR="${TOPDIR}/sources"
BUILDER_ATFDIR="${TOPDIR}/artifacts"
BUILDER_LIBDIR="${TOPDIR}/scripts/builder"

export BUILDER_CFGDIR BUILDER_PKGDIR BUILDER_SRCDIR
export BUILDER_ATFDIR BUILDER_LIBDIR BUILDER_TOPDIR

BUILDER_TMPDIR="${TOPDIR}/tmp"
if ! test -d "${BUILDER_TMPDIR}"; then
	mkdir -p "${BUILDER_TMPDIR}"
fi
export BUILDER_TMPDIR

# We save the pre-config PATH as BUILDER_PATH to be used by downstream tools.
BUILDER_PATH="${BUILDER_LIBDIR}:${PATH}"
PATH="${BUILDER_PATH}"
export BUILDER_PATH PATH

# The default SYSROOT
SYSROOT="${TOPDIR}/sysroot"

##
# This may be a little bit confusing to most.  The core issue here is the
# ability to deal with compiled-in defaults, user-defined defaults, environment
# settings, and target settings.
CFGDIR="${TOPDIR}/.builder"

# grab the default and target settings if available, let the target settings
# override the user-defined defaults.
if [ -f "${CFGDIR}/config" ]; then
	. "${CFGDIR}/config"
fi
TARGET="${TARGET:-config}"
if [ "${TARGET}" != "config" ]; then
	if [ ! -f "${CFGDIR}/${TARGET}" ]; then
		echo "error: invalid target '${TARGET}'" >&2
		exit 1
	fi
	. "${CFGDIR}/${TARGET}"
fi
export TARGET
export SYSROOT

# If the MAKE_OPTS are not set then find the number of CPU's and set them.
if [ -z "${MAKE_OPTS}" ]; then
	NUM_CPUS="$(awk '/^processor/{print$3}'< /proc/cpuinfo|sort|uniq|tail -n 1)"
	NUM_CPUS="$((${NUM_CPUS} + 1))"
	MAKE_OPTS="-j$((${NUM_CPUS} + 1))"
fi
export MAKE_OPTS

# Available to be set in the config
PROJECT="${PROJECT:-platform}"
ARCHIVE_FORMAT="${ARCHIVE_FORMAT:-tar.bz2}"
export PROJECT ARCHIVE_FORMAT

# If unspecified go ahead and ask gcc
# FIXME there has to be a more robust way to figure this out.
if [ -z "${CBUILD}" ]; then
	if command -v gcc > /dev/null 2>&1; then
		CBUILD="$(gcc -dumpmachine)"
	else
		CBUILD="$(build-dumpmachine)"
	fi
fi
export CBUILD

# 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

# FIXME Support custom commands somehow..
if [ ! -x "${BUILDER_LIBDIR}/build-${ACTION}" ]; then
	error "unknown action '${ACTION}'"
	echo "try '${0} --help'" >&2
	exit 1
fi

# query is a special case
# FIXME this is semi-busted
if [ "${ACTION}" = "query" ]; then
	exec "${BUILDER_LIBDIR}/build-query" "${@}"
fi

# If no target is given, then base our target on the current working directory,
# falling back to "${PROJECT}/all" as our default.
if [ "$#" -lt "1" ]; then
	# Are we somewhere within the pkg structure.  If this test succeeds
	# then we are at least in a category directory within the pkgdir.  Just
	# being in pkgdir is not enough to change our default argument list
	# handling.
	NAME="all"
	if [ "${PWD##${BUILDER_PKGDIR}/}" != "${PWD}" ]; then
		category="${PWD##${BUILDER_PKGDIR}/}"
		if [ "${category%%/*}" != "${category}" ]; then
			name="${category#*/}"
			category="${category%%/*}"
			NAME="${category}/${name%%/*}"
		else
			NAME="${category}/all"
		fi
		unset category
		unset name
	fi
	set -- "${NAME}"
fi

for package in "$@"; do
	# If all is specified anywhere in the argument list than just discard
	# everything else.
	if [ "${package}" = "all" ]; then
		continue
	fi
	CATEGORY="${package%%/*}"
	if [ ! -d "${BUILDER_PKGDIR}/${CATEGORY}" ]; then
		die "invalid package category '${CATEGORY}'"
	fi
	if [ "${package##*/}" != "all" ]; then
		if ! build-query --exists "${package}"; then
			exit 1
		fi
	fi
done
# sort/uniq the argument list
set -- $(for package in "$@"; do echo "${package}" ; done | sort | uniq)

# build the Makefile
trap cleanup EXIT
BUILDER_MAKEFILE="$(mktemp ${BUILDER_TMPDIR}/builder_makefile.XXXXXXXX)"
if [ ! -f "${BUILDER_MAKEFILE}" ]; then
	die "failed to generate build dependencies"
fi
export BUILDER_MAKEFILE
"${BUILDER_LIBDIR}/build-makedeps" || die "failed generate build dependencies"

packages=
for package in "$@"; do
	if [ "${package##*/}" != "all" ]; then
		package="$(build-query --pkgname "${package}")"
	fi
	package="$(echo "${package}"|tr '/-' '__')"
	packages="${packages} ${package}_${ACTION}"
done
set -- ${packages}
unset packages

# The 'tee' command will discard the exit status from 'make', so we have to
# jump through a few hoops to capture the exit status in a portable fashion.
BUILDER_PIPE="`mktemp "${BUILDER_TMPDIR}/builder_pipe.XXXXXXXX"`"
test -f "${BUILDER_PIPE}" || die 'failed to generate log-pipe placeholder'
rm -f "${BUILDER_PIPE}" && mkfifo "${BUILDER_PIPE}" || die 'failed to create log-pipe'
tee "${BUILDER_TMPDIR}/builder.log" < "${BUILDER_PIPE}" &
BUILDER_LOGGER="$!"
make -r -f "${BUILDER_MAKEFILE}" "${@}" > "${BUILDER_PIPE}" 2>&1
exit $?