1#!/bin/sh
2# vi: set sw=4 ts=4:
3
4export LC_ALL=C
5TAB="$(printf '\t')"
6NL="
7"
8
9# Verify that grep works
10echo "WORKS" | grep "WORKS" >/dev/null 2>&1
11if test $? != 0 ; then
12	echo
13	echo "grep doesn't work"
14	exit 1
15fi
16
17# Sanity check for CWD in LD_LIBRARY_PATH
18case ":${LD_LIBRARY_PATH:-unset}:" in
19(*::*|*:.:*)
20	echo
21	echo "You seem to have the current working directory in your"
22	echo "LD_LIBRARY_PATH environment variable. This doesn't work."
23	exit 1
24	;;
25esac
26
27# Sanity check for CWD in PATH. Having the current working directory
28# in the PATH makes various packages (e.g. toolchain, coreutils...)
29# build process break.
30# PATH should not contain a newline, otherwise it fails in spectacular
31# ways as soon as PATH is referenced in a package rule
32# An empty PATH is technically possible, but in practice we would not
33# even arrive here if that was the case.
34case ":${PATH:-unset}:" in
35(*::*|*:.:*)
36	echo
37	echo "You seem to have the current working directory in your"
38	echo "PATH environment variable. This doesn't work."
39	exit 1
40	;;
41(*" "*|*"${TAB}"*|*"${NL}"*)
42	printf "\n"
43	printf "Your PATH contains spaces, TABs, and/or newline (\\\n) characters.\n"
44	printf "This doesn't work. Fix you PATH.\n"
45	exit 1
46	;;
47esac
48
49check_prog_host()
50{
51	prog="$1"
52	if ! which $prog > /dev/null ; then
53		echo >&2
54		echo "You must install '$prog' on your build machine" >&2
55		exit 1
56	fi
57}
58
59# Verify that which is installed
60check_prog_host "which"
61# Verify that sed is installed
62check_prog_host "sed"
63
64# 'file' must be present and must be exactly /usr/bin/file,
65# otherwise libtool fails in incomprehensible ways.
66check_prog_host "/usr/bin/file"
67
68# Check make
69MAKE=$(which make 2> /dev/null)
70if [ -z "$MAKE" ] ; then
71	echo
72	echo "You must install 'make' on your build machine";
73	exit 1;
74fi;
75MAKE_VERSION=$($MAKE --version 2>&1 | sed -e 's/^.* \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q')
76if [ -z "$MAKE_VERSION" ] ; then
77	echo
78	echo "You must install 'make' on your build machine";
79	exit 1;
80fi;
81MAKE_MAJOR=$(echo $MAKE_VERSION | sed -e "s/\..*//g")
82MAKE_MINOR=$(echo $MAKE_VERSION | sed -e "s/^$MAKE_MAJOR\.//g" -e "s/\..*//g" -e "s/[a-zA-Z].*//g")
83if [ $MAKE_MAJOR -lt 3 ] || [ $MAKE_MAJOR -eq 3 -a $MAKE_MINOR -lt 81 ] ; then
84	echo
85	echo "You have make '$MAKE_VERSION' installed.  GNU make >=3.81 is required"
86	exit 1;
87fi;
88
89# Check host gcc
90COMPILER=$(which $HOSTCC_NOCCACHE 2> /dev/null)
91if [ -z "$COMPILER" ] ; then
92	COMPILER=$(which cc 2> /dev/null)
93fi;
94if [ -z "$COMPILER" ] ; then
95	echo
96	echo "You must install 'gcc' on your build machine";
97	exit 1;
98fi;
99
100COMPILER_VERSION=$($COMPILER -v 2>&1 | sed -n '/^gcc version/p' |
101	sed -e 's/^gcc version \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q')
102if [ -z "$COMPILER_VERSION" ] ; then
103	echo
104	echo "You must install 'gcc' on your build machine";
105	exit 1;
106fi;
107COMPILER_MAJOR=$(echo $COMPILER_VERSION | sed -e "s/\..*//g")
108COMPILER_MINOR=$(echo $COMPILER_VERSION | sed -e "s/^$COMPILER_MAJOR\.//g" -e "s/\..*//g")
109if [ $COMPILER_MAJOR -lt 4 -o $COMPILER_MAJOR -eq 4 -a $COMPILER_MINOR -lt 8 ] ; then
110	echo
111	echo "You have gcc '$COMPILER_VERSION' installed.  gcc >= 4.8 is required"
112	exit 1;
113fi;
114
115# check for host CXX
116CXXCOMPILER=$(which $HOSTCXX_NOCCACHE 2> /dev/null)
117if [ -z "$CXXCOMPILER" ] ; then
118	CXXCOMPILER=$(which c++ 2> /dev/null)
119fi
120
121if [ -z "$CXXCOMPILER" ] ; then
122	echo
123	echo "You may have to install 'g++' on your build machine"
124fi
125if [ ! -z "$CXXCOMPILER" ] ; then
126	CXXCOMPILER_VERSION=$($CXXCOMPILER -v 2>&1 | sed -n '/^gcc version/p' |
127		sed -e 's/^gcc version \([0-9\.]\)/\1/g' -e 's/[-\ ].*//g' -e '1q')
128	if [ -z "$CXXCOMPILER_VERSION" ] ; then
129		echo
130		echo "You may have to install 'g++' on your build machine"
131	fi
132fi
133
134if [ -n "$CXXCOMPILER_VERSION" ] ; then
135	CXXCOMPILER_MAJOR=$(echo $CXXCOMPILER_VERSION | sed -e "s/\..*//g")
136	CXXCOMPILER_MINOR=$(echo $CXXCOMPILER_VERSION | sed -e "s/^$CXXCOMPILER_MAJOR\.//g" -e "s/\..*//g")
137	if [ $CXXCOMPILER_MAJOR -lt 4 -o $CXXCOMPILER_MAJOR -eq 4 -a $CXXCOMPILER_MINOR -lt 8 ] ; then
138		echo
139		echo "You have g++ '$CXXCOMPILER_VERSION' installed.  g++ >= 4.8 is required"
140		exit 1
141	fi
142fi
143
144# Check bash
145# We only check bash is available, setting SHELL appropriately is done
146# in the top-level Makefile, and we mimick the same sequence here
147if   [ -n "${BASH}" ]; then :
148elif [ -x /bin/bash ]; then :
149elif [ -z "$( sh -c 'echo $BASH' )" ]; then
150	echo
151	echo "You must install 'bash' on your build machine"
152	exit 1
153fi
154
155# Check that a few mandatory programs are installed
156missing_progs="no"
157for prog in perl tar wget cpio unzip rsync bc cmp find xargs ${DL_TOOLS} ; do
158	if ! which $prog > /dev/null ; then
159		echo "You must install '$prog' on your build machine";
160		missing_progs="yes"
161		if test $prog = "svn" ; then
162			echo "  svn is usually part of the subversion package in your distribution"
163		elif test $prog = "hg" ; then
164			echo "  hg is usually part of the mercurial package in your distribution"
165		elif test $prog = "zcat" ; then
166			echo "  zcat is usually part of the gzip package in your distribution"
167		elif test $prog = "bzcat" ; then
168			echo "  bzcat is usually part of the bzip2 package in your distribution"
169		elif test $prog = "cmp" ; then
170			echo "  cmp is usually part of the diffutils package in your distribution"
171		elif test $prog = "find" ; then
172			echo "  find is usually part of the findutils package in your distribution"
173		elif test $prog = "xargs" ; then
174			echo "  xargs is usually part of the findutils package in your distribution"
175		fi
176	fi
177
178	# we need git >= 2.0.0 for shallow clones / vendoring
179	if test $prog = "git" ; then
180		GIT_VERSION="$(git --version | sed -n 's/^git version \(.*\)/\1/p')"
181		GIT_MAJOR="$(echo "${GIT_VERSION}" | cut -d . -f 1)"
182		if [ "${GIT_MAJOR}" -lt 2 ]; then
183			echo "You have git '${GIT_VERSION}' installed. Git >= 2.0.0 is required"
184			exit 1
185		fi
186	fi
187done
188
189if test "${missing_progs}" = "yes" ; then
190	exit 1
191fi
192
193PATCH_VERSION="$(patch -v 2>/dev/null | sed -n 's/^GNU patch \(.*\)/\1/p')"
194if [ -z "${PATCH_VERSION}" ] ; then
195	echo
196	echo "You must install GNU patch"
197	exit 1
198fi
199PATCH_MAJOR="$(echo "${PATCH_VERSION}" | cut -d . -f 1)"
200PATCH_MINOR="$(echo "${PATCH_VERSION}" | cut -d . -f 2)"
201if [ "${PATCH_MAJOR}" -lt 2 ] || [ "${PATCH_MAJOR}" -eq 2 -a "${PATCH_MINOR}" -lt 7 ] ; then
202	echo
203	echo "You have GNU patch '${PATCH_VERSION}' installed.  GNU patch >= 2.7 is required"
204	exit 1;
205fi
206
207if grep -q ^BR2_NEEDS_HOST_UTF8_LOCALE=y $BR2_CONFIG ; then
208	if ! which locale > /dev/null ; then
209		echo
210		echo "You need locale support on your build machine to build a toolchain supporting locales"
211		exit 1 ;
212	fi
213	if ! locale -a | grep -q -i -E 'utf-?8$' ; then
214		echo
215		echo "You need at least one UTF8 locale to build a toolchain supporting locales"
216		exit 1 ;
217	fi
218fi
219
220if grep -q ^BR2_NEEDS_HOST_JAVA=y $BR2_CONFIG ; then
221	check_prog_host "java"
222	JAVA_GCJ=$(java -version 2>&1 | grep gcj)
223	if [ ! -z "$JAVA_GCJ" ] ; then
224		echo
225		echo "$JAVA_GCJ is not sufficient to compile your package selection."
226		echo "Please install an OpenJDK/IcedTea/Oracle Java."
227		exit 1 ;
228	fi
229fi
230
231if grep -q ^BR2_HOSTARCH_NEEDS_IA32_LIBS=y $BR2_CONFIG ; then
232	if test ! -f /lib/ld-linux.so.2 ; then
233		echo
234		echo "Your Buildroot configuration uses pre-built tools for the x86 architecture,"
235		echo "but your build machine uses the x86-64 architecture without the 32 bits compatibility"
236		echo "library."
237		echo "If you're running a Debian/Ubuntu distribution, install the libc6-i386,"
238		echo "lib32stdc++6, and lib32z1 packages (or alternatively libc6:i386,"
239		echo "libstdc++6:i386, and zlib1g:i386)."
240		echo "If you're running a RedHat/Fedora distribution, install the glibc.i686 and"
241		echo "zlib.i686 packages."
242		echo "If you're running an ArchLinux distribution, install lib32-glibc."
243		echo "For other distributions, refer to the documentation on how to install the 32 bits"
244		echo "compatibility libraries."
245		exit 1
246	fi
247fi
248
249if grep -q ^BR2_HOSTARCH_NEEDS_IA32_COMPILER=y $BR2_CONFIG ; then
250	if ! echo "int main(void) {}" | gcc -m32 -x c - -o /dev/null 2>/dev/null; then
251		echo
252		echo "Your Buildroot configuration needs a compiler capable of building 32 bits binaries."
253		echo "If you're running a Debian/Ubuntu distribution, install the gcc-multilib package."
254		echo "For other distributions, refer to their documentation."
255		exit 1
256	fi
257
258	if ! echo "int main(void) {}" | g++ -m32 -x c++ - -o /dev/null 2>/dev/null; then
259		echo
260		echo "Your Buildroot configuration needs a compiler capable of building 32 bits binaries."
261		echo "If you're running a Debian/Ubuntu distribution, install the g++-multilib package."
262		echo "For other distributions, refer to their documentation."
263		exit 1
264	fi
265fi
266
267if grep -q ^BR2_NEEDS_HOST_GCC_PLUGIN_SUPPORT=y $BR2_CONFIG ; then
268	if ! echo "#include <gcc-plugin.h>" | $HOSTCXX_NOCCACHE -I$($HOSTCXX_NOCCACHE -print-file-name=plugin)/include -x c++ -c - -o /dev/null ; then
269		echo
270		echo "Your Buildroot configuration needs a host compiler capable of building gcc plugins."
271		echo "If you're running a Debian/Ubuntu distribution, install gcc-X-plugin-dev package."
272		echo "For other distributions, refer to their documentation."
273		exit 1 ;
274	fi
275fi
276
277# Check that the Perl installation is complete enough for Buildroot.
278required_perl_modules="Data::Dumper" # Needed to build host-autoconf
279required_perl_modules="$required_perl_modules English" # Used by host-libxml-parser-perl
280required_perl_modules="$required_perl_modules ExtUtils::MakeMaker" # Used by host-libxml-parser-perl
281required_perl_modules="$required_perl_modules Thread::Queue" # Used by host-automake
282required_perl_modules="$required_perl_modules FindBin" # Used by (host-)libopenssl
283required_perl_modules="$required_perl_modules IPC::Cmd" # Used by (host-)libopenssl
284
285if grep -q ^BR2_PACKAGE_LIBOPENSSL=y $BR2_CONFIG && grep -q ^BR2_s390x=y $BR2_CONFIG ; then
286    required_perl_modules="$required_perl_modules bigint"
287fi
288
289if grep -q ^BR2_PACKAGE_MOSH=y $BR2_CONFIG ; then
290    required_perl_modules="$required_perl_modules diagnostics"
291fi
292
293if grep -q ^BR2_PACKAGE_MPV=y $BR2_CONFIG ; then
294    required_perl_modules="$required_perl_modules Math::BigInt"
295    required_perl_modules="$required_perl_modules Math::BigRat"
296fi
297
298if grep -q ^BR2_PACKAGE_NETSURF=y $BR2_CONFIG ; then
299    required_perl_modules="$required_perl_modules Digest::MD5"
300fi
301
302if grep -q ^BR2_PACKAGE_WHOIS=y $BR2_CONFIG ; then
303    required_perl_modules="$required_perl_modules autodie"
304fi
305
306if grep -q -E '^BR2_PACKAGE_(WEBKITGTK|WPEWEBKIT)=y' $BR2_CONFIG ; then
307    required_perl_modules="${required_perl_modules} JSON::PP"
308fi
309
310if grep -q -E '^BR2_(PACKAGE_ACE|TARGET_SYSLINUX)=y' $BR2_CONFIG ; then
311    required_perl_modules="$required_perl_modules FileHandle"
312fi
313
314# This variable will keep the modules that are missing in your system.
315missing_perl_modules=""
316
317if grep -q ^BR2_PACKAGE_LIBXCRYPT=y $BR2_CONFIG ; then
318	# open cannot be used with require
319	if ! perl -e "use open ':std'" > /dev/null 2>&1 ; then
320		missing_perl_modules="$missing_perl_modules open"
321	fi
322fi
323
324for pm in $required_perl_modules ; do
325	if ! perl  -e "require $pm" > /dev/null 2>&1 ; then
326		missing_perl_modules="$missing_perl_modules $pm"
327	fi
328done
329
330if [ -n "$missing_perl_modules" ] ; then
331	echo "Your Perl installation is not complete enough; at least the following"
332	echo "modules are missing:"
333	echo
334	for pm in $missing_perl_modules ; do
335		printf "\t $pm\n"
336	done
337	echo
338	exit 1
339fi
340