aboutsummaryrefslogtreecommitdiff
path: root/libexec/build-query
blob: b3e23c5455d42c6d131979542644dc79cb6da37a (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
#!/usr/bin/env build

build_query_usage()
{
cat<<END_OF_HELP
usage: build-query [options] <arg>

options:
  --topdir		Builder topdir
  --toolchain		Display the toolchain environ
  --help		Display this help

package options:
  --bdepends		Package build dependancies [\$BDEPENDS]
  --bdeps-all		Build dependancies of all packages depended on
  --category		Package category [\$CATEGORY]
  --description		Package Description [\$DESCRIPTION]
  --destdir		Build-time destination directory [\$D]
  --exists		Exit with a non-zero exit-status if pkg does not exist
  --envdir		Package environment directory [unused]
  --environ		Print the package environ
  --filesdir		Package files/ directory [\$F]
  --logdir		Package log directory [\$L]
  --license		Package license [\$LICENSE]
  --name		Package name [\$NAME]
  --pkgname		Full package name [\$CATEGORY/\$NAME]
  --patches		Patches used by a package [\$PATCHES]
  --pkgdir		Path to package metadata
  --rulesfile		Path to package Buildrules file [\$RULESFILE]
  --release		Package release number [\$RELEASE]
  --rdepends		Package runtime dependancies [\$RDEPENDS]
  --rdeps-all		Runtime dependancies of all packages depended on
  --srcdir		Package source directory [\$S]
  --summary		Display package summary in specfile format
  --tmpdir		Package temporary directory [\$T]
  --src-uri		URI to package source(s) [\$SOURCE_URI]
  --version		Package version [\$VERSION]
  --var			Query specific variable from package
  --workdir		Package work directory [\$W]

END_OF_HELP
}

RECURSE_RDEPENDS='false'
recurse_bdeps()
{
	test "$#" -eq '0' && return
	if ${RECURSE_RDEPENDS}; then
		set -- "${@}" $(build query -R "${1}")
	else
		set -- "${@}" $(build query -B "${1}")
	fi

	eval query_recursive_var="$(echo "${1}"|sed -e 's,[_-/],_,g')"
	eval query_recursive_val="\$${query_recursive_var}"
	if ! test -z "${query_recursive_val}"; then
		unset query_recursive_val
		unset query_recursive_var
		return
	fi
	unset query_recursive_val

	echo "${1}"
	eval "${query_recursive_var}='${*}'"
	unset query_recursive_var

	shift
	for query_recursive_pkg; do
		recurse_bdeps "${query_recursive_pkg}"
	done
	unset
	unset query_recursive_pkg
}
recurse_rdeps()
{
	test "$#" -eq '0' && return
	RECURSE_RDEPENDS='true'
	recurse_bdeps "${@}"
}

# Look for cries for --help in the cmdline
for arg; do
	case "${arg}" in
	(-h|-help|--help) build_query_usage; exit 0;;
	esac
done

QUERY_ACTION="summary"

while [ "$#" -gt "0" ]; do
	case "${1}" in
	(-B|-bdepends|--bdepends)
		QUERY_ACTION="bdepends";;
	(-bdeps-all|--bdeps-all)
		QUERY_ACTION="bdeps_all";;
	(-c|-category|--category)
		QUERY_ACTION="category";;
	(-d|-description|--description)
		QUERY_ACTION="descr";;
	(-D|-destdir|--destdir)
		QUERY_ACTION="destdir";;
	(--exists)
		test -e "${BUILDER_PKGDIR}/${2}/Buildrules" && exit 0 || exit 1;;
	(-e|-envdir|--envdir)
		QUERY_ACTION="envdir";;
	(-E|-environ|--environ)
		QUERY_ACTION="environ";;
	(-f|-filesdir|--filesdir)
		QUERY_ACTION="filesdir";;
	(-l|-logdir|--logdir)
		QUERY_ACTION="logdir";;
	(-L|-license|--license)
		QUERY_ACTION="license";;
	(-n|-name|--name)
		QUERY_ACTION="name";;
	(-N|-pkgname|--pkgname)
		QUERY_ACTION="pkgname";;
	(-p|-patches|--patches)
		QUERY_ACTION="patches";;
	(-P|-pkgdir|--pkgdir)
		QUERY_ACTION="pkgdir";;
	(-r|-rulefile|--rulesfile)
		QUERY_ACTION="rulesfile";;
	(--release)
		QUERY_ACTION="release";;
	(-R|-rdepends|--rdepends)
		QUERY_ACTION="rdepends";;
	(-rdeps-all|--rdeps-all)
		QUERY_ACTION="rdeps_all";;
	(-s|-srcdir|--srcdir)
		QUERY_ACTION="srcdir";;
	(-S|-summary|--summary)
		QUERY_ACTION="summary";;
	(-toolchain|--toolchain)
		QUERY_ACTION="toolchain";;
	(-t|-topdir|--topdir)
		QUERY_ACTION="topdir";;
	(-T|-tmpdir|--tmpdir)
		QUERY_ACTION="tmpdir";;
	(-u|-source-uri|--src-uri)
		QUERY_ACTION="src_uri";;
	(-v|-version|--version)
		QUERY_ACTION="version";;
	(-V|-var|--var)
		shift
		if [ "$#" -lt "1" ]; then
			error "no variable specified"
			echo "try '${##*//} --help'" >&2
			exit 1
		fi

		QUERY_ACTION="var"
		QUERY_VAR="${1}"
		;;
	(-w|-workdir|--workdir)
		QUERY_ACTION="workdir";;
	(-*)	error "unknown query '${1}'"
		echo "try '${0##*/} --help'" >&2
		exit 1
		;;
	(*)	break;;
	esac
	shift
done

# FIXME move these to 'build-config'. 'build-query' is for package data
case "${QUERY_ACTION}" in
(topdir)
	echo "${BUILDER_TOPDIR}"
	exit 0;;

(toolchain)

	if [ "$#" -gt "0" ] && [ "${1}" != "${CHOST}" ]; then
		CROSS_COMPILE="${1}-"
		shift
	else
		CROSS_COMPILE="${CHOST}-"
	fi

	CC="${CROSS_COMPILE}gcc"
	CXX="${CROSS_COMPILE}g++"

	cat<<-EOF
	CC="${CC}"
	CXX="${CXX}"
	CROSS_COMPILE="${CROSS_COMPILE}"
	EOF
	export CC CXX CROSS_COMPILE

	# Note: we do not set AS since we often use gcc as the assembler

	for cmd in ar ld strip ranlib autom4te autoconf autoheader automake \
		aclocal autopoint libtool libtoolize; do
		var="$(echo "${cmd}"|tr '[a-z]' '[A-Z]')"
		if command -v "${CROSS_COMPILE}${cmd}" > /dev/null 2>&1; then
			echo  "${var}=${CROSS_COMPILE}${cmd}"
		else
			echo "${var}=${cmd}"
		fi
		echo "export ${var}"
	done

	exit 0;;
esac

if test "$#" -eq "0"; then
	die "no package specified"
fi

load_rules "${1}"

# Allow the developer to hijack the SRC_URI with a checked out repository
# FIXME this causes problems for various dep targets
if [ -d "${BUILDER_PKGDIR}/${CATEGORY}/${NAME}/source" ]; then
	SOURCE_URI="file://${BUILDER_PKGDIR}/${CATEGORY}/${NAME}/source"
fi

case "${QUERY_ACTION}" in
(name)		echo "${NAME}";exit 0;;
(category)	echo "${CATEGORY}";exit 0;;
(pkgname)	echo "${CATEGORY}/${NAME}";exit 0;;
(pkgdir)	echo "${BUILDER_PKGDIR}/${CATEGORY}/${NAME}";exit 0;;
(rulesfile)	echo "${RULESFILE}";exit 0;;
(filesdir)	echo "${F}";exit 0;;
(workdir)	echo "${W}";exit 0;;
(logdir)	echo "${L}";exit 0;;
(envdir)	echo "${E}";exit 0;;
(tmpdir)	echo "${T}";exit 0;;
(destdir)	echo "${D}";exit 0;;


(srcdir)	echo "${S}";;
(src_uri)	echo "${SOURCE_URI}";;
(bdepends)	echo "$(echo ${BDEPENDS})";;
(bdeps_all)	recurse_bdeps ${BDEPENDS};;
(rdepends)	echo "$(echo ${RDEPENDS})";;
(rdeps_all)	recurse_rdeps ${RDEPENDS};;
(descr)		echo "${DESCRIPTION}";;
(version)	echo "${VERSION}";;
(release)	echo "${RELEASE}";;
(patches)	echo "${PATCHES}";;
(license)	echo "${LICENSE}";;

# Allow the caller to specify the variable to report.
# FIXME may need to protect our existing environ from being poked.
(var)		if [ -z "${QUERY_VAR}" ]; then
			die "no variable specified"
		fi
		eval "echo \"\$${QUERY_VAR}\"";;

# All the fun environ data necessary for grabbing a single package.  We ALWAYS
# report all the variables, even if unset.  This allows a caller to use this
# command to set their own environ and automagically clear any variables which
# are already set in their environment.
(environ)
		cat<<-EOF
		TARGET="${TARGET}"
		TOPDIR="${BUILDER_TOPDIR}"
		SYSROOT="${SYSROOT}"
		CBUILD="${CBUILD}"
		CHOST="${CHOST}"
		ARCH="${ARCH}"
		NAME="${NAME}"
		CATEGORY="${CATEGORY}"
		RULESFILE="${RULESFILE}"
		VERSION="${VERSION}"
		RELEASE="${RELEASE}"
		DESCRIPTION="${DESCRIPTION}"
		LICENSE="${LICENSE}"
		SOURCE_URI="${SOURCE_URI}"
		EOF

		if [ ! -z "${NOCLEAN}" ]; then
			echo "NOCLEAN=\"${NOCLEAN}\""
		else
			echo "NOCLEAN="
		fi
		if [ ! -z "${PATCHES}" ]; then
			echo "PATCHES=\"${PATCHES}\""
		else
			echo "PATCHES="
		fi
		if [ ! -z "${RDEPENDS}" ]; then
			echo "RDEPENDS=\"${RDEPENDS}\""
		else
			echo "RDEPENDS="
		fi
		if [ ! -z "${BDEPENDS}" ]; then
			echo "BDEPENDS=\"${BDEPENDS}\""
		else
			echo "BDEPENDS="
		fi
		cat<<-EOF
		F="${F}"
		W="${W}"
		L="${L}"
		E="${E}"
		T="${T}"
		D="${D}"
		S="${S}"
		EOF
		;;

# Basic summary command
(summary)
		echo "Name:		${CATEGORY}/${NAME}"
		echo "Version:	${VERSION}"
		echo "Release:	${RELEASE}"
		echo "License:	${LICENSE}"
		echo "Source:		${SOURCE_URI}"
		if [ ! -z "${RDEPENDS}" ]; then
			echo "Depends:		${RDEPENDS}"
		fi
		if [ ! -z "${BDEPENDS}" ]; then
			echo "BuildDeps:	${BDEPENDS}"
		fi

		cat <<-EOF

		${DESCRIPTION}
		EOF
		;;

(*)		die "unhandled query action '${QUERY_ACTION}'";;
esac

# vim: filetype=sh