152a38012Sejakowatz#!/bin/sh
252a38012Sejakowatz#
3338b8dc3SIngo Weinhold# configure [ <options> ]
4614026d7Sshatty
5022fa244SIngo Weinhold# usage
6022fa244SIngo Weinhold#
7022fa244SIngo Weinhold# Prints usage.
8022fa244SIngo Weinhold#
9022fa244SIngo Weinholdusage()
10022fa244SIngo Weinhold{
11022fa244SIngo Weinhold	cat << EOF
12022fa244SIngo Weinhold
13022fa244SIngo WeinholdUsage: $0 <options>
14022fa244SIngo Weinholdoptions:
15825700d3SAugustin Cavalier  --help                      Prints out this help.
16825700d3SAugustin Cavalier  --update                    Re-runs last configure invocation [must be given
17825700d3SAugustin Cavalier                              as first option!]
18e173a1ecSIngo Weinhold  --bootstrap <haikuporter> <HaikuPorts cross repo> <HaikuPorts repo>
19e173a1ecSIngo Weinhold                              Prepare for a bootstrap build. No pre-built
20e173a1ecSIngo Weinhold                              packages will be used, instead they will be built
21e173a1ecSIngo Weinhold                              from the sources (in several phases).
22e173a1ecSIngo Weinhold                              <haikuporter> is the path to the haikuporter tool
23e173a1ecSIngo Weinhold                              suitable for the host platform.
24e173a1ecSIngo Weinhold                              <HaikuPorts cross repo> is the path to a checked
25e173a1ecSIngo Weinhold                              out HaikuPorts cross-compilation repository.
26e173a1ecSIngo Weinhold                              <HaikuPorts repo> is the path to a checked out
27e173a1ecSIngo Weinhold                              HaikuPorts repository.
28c0e8cc1aSIngo Weinhold  --build-cross-tools <arch> [ <build tools dir> ]
29338b8dc3SIngo Weinhold                              Assume cross compilation. <build tools dir>
30338b8dc3SIngo Weinhold                              defines the location of the build tools sources.
31338b8dc3SIngo Weinhold                              They will be compiled and placed in the output
32a5b60fa8SOliver Tappe                              directory under "cross-tools". The HAIKU_* tools
33a5b60fa8SOliver Tappe                              variables will be set accordingly.
3420ab75e6SIngo Weinhold                              <arch> specifies the target architecture, either
35fb4cc984SAugustin Cavalier                              "x86_gcc2", "x86", "x86_64", "ppc", "m68k", "arm", "arm64"
36c0e8cc1aSIngo Weinhold                              This option and --cross-tools-prefix can be
37c0e8cc1aSIngo Weinhold                              specified multiple times. The first cross tools
38c0e8cc1aSIngo Weinhold                              specify the primary tools, the subsequent ones the
39c0e8cc1aSIngo Weinhold                              secondary tools (for "hybrid" images).
40c0e8cc1aSIngo Weinhold                              For the first --build-cross-tools the
41c0e8cc1aSIngo Weinhold                              <build tools dir> argument must be specified and
42c0e8cc1aSIngo Weinhold                              for the subsequent ones it must be omitted.
43eccc7665SIngo Weinhold  --cross-tools-prefix <prefix>
44eccc7665SIngo Weinhold                              Assume cross compilation. <prefix> should be a
45eccc7665SIngo Weinhold                              path to the directory where the cross
46eccc7665SIngo Weinhold                              compilation tools are located, plus the platform
470da9c208SIngo Weinhold                              prefix, e.g. "/path/to/tools/i586-pc-haiku-".
48eccc7665SIngo Weinhold                              This overrides the HAIKU_* tool variables.
49a66c32ddSIngo Weinhold  --distro-compatibility <level>
50a66c32ddSIngo Weinhold                              The distribution's level of compatibility with
51a66c32ddSIngo Weinhold                              the official Haiku distribution. The generated
52a66c32ddSIngo Weinhold                              files will contain the respective trademarks
53a66c32ddSIngo Weinhold                              accordingly.
54a66c32ddSIngo Weinhold                              official -- the official Haiku distribution.
55a66c32ddSIngo Weinhold                              compatible -- a Haiku Compatible (tm) distro.
56a66c32ddSIngo Weinhold                              default -- any other distro (default value).
572ebcd86dSIngo Weinhold  --host-only                 Configure for building tools for the build host
582ebcd86dSIngo Weinhold                              only. Haiku cannot be built when configured like
592ebcd86dSIngo Weinhold                              this.
6076fa0be3SIngo Weinhold  --include-sources           Includes the source code of projects that require
6176fa0be3SIngo Weinhold                              either an offer of source code or a copy of the
6276fa0be3SIngo Weinhold                              patched sources. This is preferable when
6376fa0be3SIngo Weinhold                              distributing on physical mediums.
641974210fSFrançois Revol  --include-3rdparty          Include 3rdparty/ in the build system.
654880ae01SJessica Hamilton  -j<n>                       Only relevant for --build-cross-tools. Is passed
664880ae01SJessica Hamilton                              on to the make building the build tools.
67aa2e5ecaSOliver Tappe  --no-downloads              Do not download anything. Useful when trying to
68aa2e5ecaSOliver Tappe                              bootstrap and build Haiku from source only.
69483b28e4SIngo Weinhold  --target-arch <arch>        Haiku only: Specify the target architecture to
70483b28e4SIngo Weinhold                              build for. Must be one of the architectures of the
71483b28e4SIngo Weinhold                              host system. The installed build tools for that
72483b28e4SIngo Weinhold                              architecture will be used.
73483b28e4SIngo Weinhold                              This option can be specified multiple times. The
74483b28e4SIngo Weinhold                              first occurrence specifies the primary
75483b28e4SIngo Weinhold                              architecture of the Haiku to build, subsequent
76483b28e4SIngo Weinhold                              ones the secondary architectures.
77d73993ffSAugustin Cavalier  --use-clang <arch>          Build with host Clang instead of GCC cross
78d73993ffSAugustin Cavalier                              compiler, targeting <arch>
79eccc7665SIngo Weinhold  --use-gcc-pipe              Build with GCC option -pipe. Speeds up the build
805a34a443SFrançois Revol                              process, but uses more memory.
81ed38d2efSJerome Duval  --use-gcc-graphite          Build with GCC Graphite engine for loop
82a5c952dbSAugustin Cavalier                              optimizations. (Only for GCC 4+.)
830385b065SIngo Weinhold  --use-32bit                 Use -m32 flag on 64bit host gcc compiler.
84fc2c93fbSAugustin Cavalier  --no-full-xattr             Do not use Linux/*BSD/Darwin's native extended file
85fc2c93fbSAugustin Cavalier                              attributes as Haiku attributes. If they are still
86fc2c93fbSAugustin Cavalier                              available, they will be used to store hashes for
87fc2c93fbSAugustin Cavalier                              the attribute emulation layer.
88fc2c93fbSAugustin Cavalier  --no-xattr                  Do not use Linux/*BSD/Darwin's native extended file
89fc2c93fbSAugustin Cavalier                              attributes for Haiku extended attributes at all,
90fc2c93fbSAugustin Cavalier                              even if they are available.
914da6cf84SFrançois Revol  --with-gdb <gdb sources dir>
924da6cf84SFrançois Revol                              specify the path to a GDB source dir, to build
934da6cf84SFrançois Revol                              GDB for each arch we build the cross-tools for.
94338b8dc3SIngo Weinhold
95338b8dc3SIngo Weinholdenvironment variables:
9663701e7dSOliver Tappe  CC                          The host compiler. Defaults to "gcc".
9765706842SAugustin Cavalier  HAIKU_AR_<arch>             The static library archiver for <arch>.
98c0e8cc1aSIngo Weinhold                              Defaults to "ar".
9965706842SAugustin Cavalier  HAIKU_CC_<arch>             The compiler for <arch>. Defaults to "gcc".
10065706842SAugustin Cavalier  HAIKU_LD_<arch>             The <arch> linker. Defaults to "ld".
10165706842SAugustin Cavalier  HAIKU_OBJCOPY_<arch>        The <arch> objcopy to be used. Defaults to
102c0e8cc1aSIngo Weinhold                              "objcopy".
10365706842SAugustin Cavalier  HAIKU_RANLIB_<arch>         The static library indexer for <arch>. Defaults
104c0e8cc1aSIngo Weinhold                              to "ranlib".
10565706842SAugustin Cavalier  HAIKU_STRIP_<arch>          The <arch> strip command. Defaults to "strip".
106a04a520bSJonathan Schleifer  HAIKU_NASM                  The nasm assembler (x86 and x86_64 only).
107c0e8cc1aSIngo Weinhold  HAIKU_CPPFLAGS_<arch>       The preprocessor flags for target architecture
108c0e8cc1aSIngo Weinhold                              <arch>. Defaults to "".
109c0e8cc1aSIngo Weinhold  HAIKU_CCFLAGS_<arch>        The C flags for target architecture <arch>.
110c0e8cc1aSIngo Weinhold                              Defaults to "".
111c0e8cc1aSIngo Weinhold  HAIKU_CXXFLAGS_<arch>       The C++ flags for target architecture <arch>.
112c0e8cc1aSIngo Weinhold                              Defaults to "".
113252e4f62SAugustin Cavalier  HAIKU_LINKFLAGS_<arch>      The flags passed to the compiler when linking for
114252e4f62SAugustin Cavalier                              target architecture <arch>. Defaults to "".
115c0e8cc1aSIngo Weinhold  HAIKU_LDFLAGS_<arch>        The linker flags for target architecture <arch>.
116c0e8cc1aSIngo Weinhold                              Defaults to "".
117c0e8cc1aSIngo Weinhold  HAIKU_ARFLAGS_<arch>        The flags passed to HAIKU_AR for target
118c0e8cc1aSIngo Weinhold                              architecture <arch> for archiving. Defaults to
119c0e8cc1aSIngo Weinhold                              "cru".
120c0e8cc1aSIngo Weinhold  HAIKU_UNARFLAGS_<arch>      The flags passed to HAIKU_AR for target
121c0e8cc1aSIngo Weinhold                              architecture <arch> for unarchiving. Defaults to
122c0e8cc1aSIngo Weinhold                              "x".
123c0e8cc1aSIngo Weinhold
124c0e8cc1aSIngo WeinholdNon-default output directories:
1255abd9a46SIngo Weinhold  By default all objects, build configuration, and other related files are
12664f6da31SIngo Weinhold  stored in /path/to/haiku_source/generated.  To store objects in a non-default
1275abd9a46SIngo Weinhold  location, run "../../relative/path/to/haiku_source/configure <options>" from
1285abd9a46SIngo Weinhold  within your non-default location.  "jam [ options ] targets" can then be run
1295abd9a46SIngo Weinhold  directly inside your non-default location.  Another option is to invoke "jam
1305abd9a46SIngo Weinhold  [ options ] targets" from within haiku_source.  This can be accomplished by
1315abd9a46SIngo Weinhold  either "export HAIKU_OUTPUT_DIR=your non-default location" before invoking
1325abd9a46SIngo Weinhold  jam or by creating a symlink of haiku_source/generated pointing to your
13364f6da31SIngo Weinhold  non-default location and running jam.
13464f6da31SIngo Weinhold
13564f6da31SIngo Weinhold
136022fa244SIngo WeinholdEOF
137022fa244SIngo Weinhold}
138022fa244SIngo Weinhold
139022fa244SIngo Weinhold# assertparam
140022fa244SIngo Weinhold#
141022fa244SIngo Weinhold# Checks whether at least one parameter is left.
142022fa244SIngo Weinhold#
143022fa244SIngo Weinholdassertparam()
144022fa244SIngo Weinhold{
14520ab75e6SIngo Weinhold	if [ $2 -lt 2 ]; then
146022fa244SIngo Weinhold		echo $0: \`$1\': Parameter expected.
147022fa244SIngo Weinhold		exit 1
148022fa244SIngo Weinhold	fi
149022fa244SIngo Weinhold}
150022fa244SIngo Weinhold
15120ab75e6SIngo Weinhold# assertparams
15220ab75e6SIngo Weinhold#
15320ab75e6SIngo Weinhold# Checks whether at least a certain number of parameters is left.
15420ab75e6SIngo Weinhold#
15520ab75e6SIngo Weinholdassertparams()
15620ab75e6SIngo Weinhold{
15720ab75e6SIngo Weinhold	if [ $3 -le $2 ]; then
15820ab75e6SIngo Weinhold		echo $0: \`$1\': Not enough parameters.
15920ab75e6SIngo Weinhold		exit 1
16020ab75e6SIngo Weinhold	fi
16120ab75e6SIngo Weinhold}
16220ab75e6SIngo Weinhold
163e173a1ecSIngo Weinhold# absolute_path
164e173a1ecSIngo Weinhold#
165e173a1ecSIngo Weinhold# returns the absolute path of a given path.
166e173a1ecSIngo Weinhold#
167e173a1ecSIngo Weinholdabsolute_path()
168e173a1ecSIngo Weinhold{
1696a4f3954SFrançois Revol	if [ "x$1" != "x${1#/}" ]; then
170e173a1ecSIngo Weinhold		echo "$1"
171e173a1ecSIngo Weinhold	else
172e173a1ecSIngo Weinhold		echo "`pwd`/$1"
173e173a1ecSIngo Weinhold	fi
174e173a1ecSIngo Weinhold}
175e173a1ecSIngo Weinhold
17639d444a3SArvind S Raj# check_dir_exists
17739d444a3SArvind S Raj#
17839d444a3SArvind S Raj# check if a directory exists or not
17939d444a3SArvind S Raj#
18039d444a3SArvind S Rajcheck_dir_exists()
18139d444a3SArvind S Raj{
18239d444a3SArvind S Raj	if [ -d "$1" ]; then
18339d444a3SArvind S Raj		return 0
18439d444a3SArvind S Raj	else
18539d444a3SArvind S Raj		return 1
18639d444a3SArvind S Raj	fi
18739d444a3SArvind S Raj}
18839d444a3SArvind S Raj
189502882dbSJessica Hamilton# check_file_exists
190502882dbSJessica Hamilton#
191502882dbSJessica Hamilton# check if a file exists or not
192502882dbSJessica Hamilton#
193502882dbSJessica Hamiltoncheck_file_exists()
194502882dbSJessica Hamilton{
195502882dbSJessica Hamilton	if [ -f "$1" ]; then
196502882dbSJessica Hamilton		return 0
197502882dbSJessica Hamilton	else
198502882dbSJessica Hamilton		return 1
199502882dbSJessica Hamilton	fi
200502882dbSJessica Hamilton}
201502882dbSJessica Hamilton
2029a001156SOliver Tappe# real_path
2039a001156SOliver Tappe#
2049a001156SOliver Tappe# returns the realpath of a symbolic link.
2059a001156SOliver Tappe#
2069a001156SOliver Tappereal_path()
2079a001156SOliver Tappe{
2089a001156SOliver Tappe	perl -MCwd=realpath -e'print realpath($ARGV[0]), "\n"' "$1"
2099a001156SOliver Tappe}
2109a001156SOliver Tappe
211e5d446fcSAlexander von Gluck IV# valid_toolchain
212e5d446fcSAlexander von Gluck IV#
2131f839e89Swaddlesplash# check if a toolchain is valid
214e5d446fcSAlexander von Gluck IV#
215e5d446fcSAlexander von Gluck IVvalid_toolchain()
216e5d446fcSAlexander von Gluck IV{
217e5d446fcSAlexander von Gluck IV	TRIPLET="$1"
218e5d446fcSAlexander von Gluck IV	BASE="$2"
2191f839e89Swaddlesplash	SOURCE="$3"
220e5d446fcSAlexander von Gluck IV	if [ ! -d "$BASE" ]; then
2211f839e89Swaddlesplash		return 1
222e5d446fcSAlexander von Gluck IV	fi
223e5d446fcSAlexander von Gluck IV	if [ -f "$BASE/bin/$TRIPLET-gcc" ]; then
2241f839e89Swaddlesplash		[ "$BASE/bin/$TRIPLET-gcc" -nt "$SOURCE/legacy/gcc/configure" ] && \
2251f839e89Swaddlesplash			[ "$BASE/bin/$TRIPLET-gcc" -nt "$SOURCE/gcc/gcc/configure" ]
2261f839e89Swaddlesplash		return $?
227e5d446fcSAlexander von Gluck IV	fi
2281f839e89Swaddlesplash	return 1
229e5d446fcSAlexander von Gluck IV}
230e5d446fcSAlexander von Gluck IV
23109c5682dSIngo Weinhold# standard_gcc_settings
23209c5682dSIngo Weinhold#
23309c5682dSIngo Weinhold# Sets the variables for a GCC platform.
23409c5682dSIngo Weinhold#
23509c5682dSIngo Weinholdstandard_gcc_settings()
23609c5682dSIngo Weinhold{
23765706842SAugustin Cavalier	local gcc="$1"
238c0e8cc1aSIngo Weinhold
239138f5fb4SIngo Weinhold	if which greadlink > /dev/null 2>&1; then
240b29b48b1SMatt Madia		readlink="greadlink -e"
241138f5fb4SIngo Weinhold	elif which realpath > /dev/null 2>&1; then
242b29b48b1SMatt Madia		readlink=realpath
2439a001156SOliver Tappe	elif readlink -e / > /dev/null 2>&1; then
244b29b48b1SMatt Madia		readlink="readlink -e"
2459a001156SOliver Tappe	else
2469a001156SOliver Tappe		readlink=real_path
247b4b6d83cSMatt Madia	fi
248138f5fb4SIngo Weinhold
24909c5682dSIngo Weinhold	# PLATFORM_LINKLIBS
250c0e8cc1aSIngo Weinhold	local gcclib=`$gcc -print-libgcc-file-name`
251c0e8cc1aSIngo Weinhold	local gccdir=`dirname ${gcclib}`
2525b0f7b1bSOliver Tappe
253c0e8cc1aSIngo Weinhold	local gccRawVersion=`$gcc -dumpversion`
254c0e8cc1aSIngo Weinhold	local gccMachine=`$gcc -dumpmachine`
2550385b065SIngo Weinhold
256fc8967f1SIngo Weinhold	# determine architecture from machine triple
257c0e8cc1aSIngo Weinhold	case $gccMachine in
258c0e8cc1aSIngo Weinhold		arm-*)		targetCpu=arm;;
259fb4cc984SAugustin Cavalier		arm64-*)	targetCpu=arm64;;
260c0e8cc1aSIngo Weinhold		i?86-*)		targetCpu=x86;;
261c0e8cc1aSIngo Weinhold		m68k-*)		targetCpu=m68k;;
262c0e8cc1aSIngo Weinhold		powerpc-*)	targetCpu=ppc;;
263c0e8cc1aSIngo Weinhold		x86_64-*)	targetCpu=x86_64;;
264fc8967f1SIngo Weinhold		*)
265c0e8cc1aSIngo Weinhold			echo "Unsupported gcc target machine: $gccMachine" >&2
266fc8967f1SIngo Weinhold			exit 1
267fc8967f1SIngo Weinhold			;;
268fc8967f1SIngo Weinhold	esac
269fc8967f1SIngo Weinhold
270c0e8cc1aSIngo Weinhold	local targetArch=$targetCpu
271fc8967f1SIngo Weinhold
272c0e8cc1aSIngo Weinhold	case $gccRawVersion in
2736cc8eecfSOliver Tappe		2.9*)
2745b0f7b1bSOliver Tappe			# check for correct (most up-to-date) legacy compiler and complain
2755b0f7b1bSOliver Tappe			# if an older one is installed
276c0e8cc1aSIngo Weinhold			if [ $gccRawVersion != $haikuRequiredLegacyGCCVersion ]; then
2775b0f7b1bSOliver Tappe				echo "GCC version $haikuRequiredLegacyGCCVersion is required!";
2785b0f7b1bSOliver Tappe				echo "Please download it from www.haiku-os.org...";
2795b0f7b1bSOliver Tappe				exit 1;
2805b0f7b1bSOliver Tappe			fi
281aaeadfcbSAlex Smith
282c0e8cc1aSIngo Weinhold			targetArch=x86_gcc2
283fc8967f1SIngo Weinhold			;;
2848b5934c9SIngo Weinhold	esac
28565ad1ba3SAlex Smith
286c0e8cc1aSIngo Weinhold	local bootLibgcc
287c0e8cc1aSIngo Weinhold	local bootLibSupCxx
28866a7f29fSJérôme Duval	local bootCxxHeaders
289c0e8cc1aSIngo Weinhold	case $gccMachine in
2904591ebc9SAlex Smith		x86_64-*)
29166a7f29fSJérôme Duval			# Boot loader is 32-bit, need the 32-bit libs and c++ config
29211ae3c5fSOliver Tappe			bootLibgcc=`$gcc -m32 -print-file-name=libgcc.a`
293c0e8cc1aSIngo Weinhold			bootLibSupCxx=`$gcc -m32 -print-file-name=libsupc++.a`
29466a7f29fSJérôme Duval
29566a7f29fSJérôme Duval			local headersBase=$gccdir/../../../..
29666a7f29fSJérôme Duval			local headers=$headersBase/$gccMachine/include/c++/$gccRawVersion
29766a7f29fSJérôme Duval			if [ ! -d $headers ]; then
29866a7f29fSJérôme Duval				headers=$headersBase/include/c++/$gccRawVersion
29966a7f29fSJérôme Duval			fi
30066a7f29fSJérôme Duval			bootCxxHeaders="$headers/$gccMachine/32"
301fc8967f1SIngo Weinhold			;;
3028b5934c9SIngo Weinhold	esac
303c0e8cc1aSIngo Weinhold
304c0e8cc1aSIngo Weinhold	# determine whether graphite loop optimization should/can be used
305c0e8cc1aSIngo Weinhold	local useGraphite=`get_variable HAIKU_USE_GCC_GRAPHITE_$targetCpu`
306c0e8cc1aSIngo Weinhold	if [ -z "$useGraphite" ]; then
307c0e8cc1aSIngo Weinhold		useGraphite=$useGccGraphiteDefault
308c0e8cc1aSIngo Weinhold	fi
309c0e8cc1aSIngo Weinhold
310c0e8cc1aSIngo Weinhold	if [ "$useGraphite" != 0 ]; then
311c0e8cc1aSIngo Weinhold		UNUSED=`echo "int main() {}" | $gcc -xc -c -floop-block - 2>&1`
312c0e8cc1aSIngo Weinhold		if [ $? != 0 ]; then
313c0e8cc1aSIngo Weinhold			echo "GCC Graphite loop optimizations cannot be used on $targetArch"
314c0e8cc1aSIngo Weinhold			useGraphite=0
315c0e8cc1aSIngo Weinhold		fi
316c0e8cc1aSIngo Weinhold	fi
317c0e8cc1aSIngo Weinhold
318c0e8cc1aSIngo Weinhold	set_variable HAIKU_CPU_$targetArch $targetCpu
319c0e8cc1aSIngo Weinhold
320c0e8cc1aSIngo Weinhold	get_build_tool_path CC_$targetArch "$gcc"
321a7bddff1SJonathan Schleifer	set_variable HAIKU_CC_IS_CLANG_$targetArch $useClang
322c0e8cc1aSIngo Weinhold	set_variable HAIKU_GCC_RAW_VERSION_$targetArch $gccRawVersion
323c0e8cc1aSIngo Weinhold	set_variable HAIKU_GCC_MACHINE_$targetArch $gccMachine
324c0e8cc1aSIngo Weinhold	set_variable HAIKU_GCC_LIB_DIR_$targetArch $gccdir
32511ae3c5fSOliver Tappe	set_variable HAIKU_BOOT_CXX_HEADERS_DIR_$targetArch "$bootCxxHeaders"
326c0e8cc1aSIngo Weinhold	set_variable HAIKU_BOOT_LIBSUPCXX_$targetArch "$bootLibSupCxx"
327c0e8cc1aSIngo Weinhold	set_variable HAIKU_BOOT_LIBGCC_$targetArch $bootLibgcc
328c0e8cc1aSIngo Weinhold	set_variable HAIKU_USE_GCC_GRAPHITE_$targetArch $useGraphite
329c0e8cc1aSIngo Weinhold
330c0e8cc1aSIngo Weinhold	standard_gcc_settings_targetArch=$targetArch
331c0e8cc1aSIngo Weinhold}
332c0e8cc1aSIngo Weinhold
333c0e8cc1aSIngo Weinhold# set_variable
334c0e8cc1aSIngo Weinhold#
335c0e8cc1aSIngo Weinhold# Set the value of a variable.
336c0e8cc1aSIngo Weinhold#
337c0e8cc1aSIngo Weinholdset_variable()
338c0e8cc1aSIngo Weinhold{
339c0e8cc1aSIngo Weinhold	eval "$1=\"$2\""
340c0e8cc1aSIngo Weinhold}
341c0e8cc1aSIngo Weinhold
342c0e8cc1aSIngo Weinhold# get_variable
343c0e8cc1aSIngo Weinhold#
344c0e8cc1aSIngo Weinhold# Echo the value of a variable.
345c0e8cc1aSIngo Weinhold#
346c0e8cc1aSIngo Weinholdget_variable()
347c0e8cc1aSIngo Weinhold{
348c0e8cc1aSIngo Weinhold	eval "echo \${$1}"
349338b8dc3SIngo Weinhold}
350338b8dc3SIngo Weinhold
351338b8dc3SIngo Weinhold# set_default_value
352338b8dc3SIngo Weinhold#
353338b8dc3SIngo Weinhold# Set the value for a variable, if no value is set yet.
354338b8dc3SIngo Weinhold#
355338b8dc3SIngo Weinholdset_default_value()
356338b8dc3SIngo Weinhold{
3570838a930SJérôme Duval	eval "$1=\${$1-$2}"
358338b8dc3SIngo Weinhold}
359338b8dc3SIngo Weinhold
360338b8dc3SIngo Weinhold# get_build_tool_path
361338b8dc3SIngo Weinhold#
362338b8dc3SIngo Weinhold# Gets a usable absolute path of a build tool.
363338b8dc3SIngo Weinhold#
364338b8dc3SIngo Weinholdget_build_tool_path()
365338b8dc3SIngo Weinhold{
366338b8dc3SIngo Weinhold	local var="HAIKU_$1"
36765706842SAugustin Cavalier	local varval="`get_variable $var`"
36865706842SAugustin Cavalier	local cmd="$2"
369d73993ffSAugustin Cavalier
370e042a53bSAugustin Cavalier	if [ ! -z "$varval" ]; then
371d73993ffSAugustin Cavalier		# this variable is already set (probably by user) so grab its contents
372e042a53bSAugustin Cavalier		cmd=$varval
373d73993ffSAugustin Cavalier	fi
374d73993ffSAugustin Cavalier
375d73993ffSAugustin Cavalier	local path=${cmd%% *}
3760385b065SIngo Weinhold
377338b8dc3SIngo Weinhold	if [ -f "$path" ]; then
37865706842SAugustin Cavalier		# get absolute path from relative path
379a559f87aSFrançois Revol		local oldPwd="`pwd`"
380a559f87aSFrançois Revol		cd "`dirname "$path"`"
381a559f87aSFrançois Revol		path="`pwd`/`basename "$path"`"
382338b8dc3SIngo Weinhold		cd $oldPwd
383338b8dc3SIngo Weinhold	else
3847fe2e06cSMatt Madia		which "$path" > /dev/null 2>&1 || {
385d73993ffSAugustin Cavalier			echo "Build tool \"$path\" not found (maybe specify it in $var?)" >&2
386338b8dc3SIngo Weinhold			exit 1
387338b8dc3SIngo Weinhold		}
388b45cb282SAxel Dörfler	fi
3890385b065SIngo Weinhold
39065706842SAugustin Cavalier	if test "${cmd#* }" != "$cmd"; then
39165706842SAugustin Cavalier		# $cmd contains arguments, so preserve them (and only them)
39265706842SAugustin Cavalier		cmd=${cmd#* }
39365706842SAugustin Cavalier	else
39465706842SAugustin Cavalier		# $cmd does not contain arguments, so unset it
39565706842SAugustin Cavalier		cmd=
39665706842SAugustin Cavalier	fi
39765706842SAugustin Cavalier	eval "$var=\"$path $cmd\""
39809c5682dSIngo Weinhold}
39909c5682dSIngo Weinhold
400a5c952dbSAugustin Cavalier# check_native_xattrs
401a5c952dbSAugustin Cavalier#
402a5c952dbSAugustin Cavalier# Checks the host platform's support for extended attributes.
403a5c952dbSAugustin Cavalier# 0: no support, 1: only enough for xattr-ref, 2: full support
404a5c952dbSAugustin Cavalier#
405a5c952dbSAugustin Cavaliercheck_native_xattrs()
406a5c952dbSAugustin Cavalier{
407a5c952dbSAugustin Cavalier	local xattr_set=
408a5c952dbSAugustin Cavalier	local xattr_set_args=
409a5c952dbSAugustin Cavalier	local xattr_get=
410a5c952dbSAugustin Cavalier	local xattr_get_args=
411a5c952dbSAugustin Cavalier	case $HOST_PLATFORM in
412503ef6d4SAugustin Cavalier		haiku_host)
413503ef6d4SAugustin Cavalier			xattr_set="addattr"; xattr_set_args="\$NAME \"\$VALUE\""
414503ef6d4SAugustin Cavalier			xattr_get="catattr"; xattr_get_args="\$NAME"
415503ef6d4SAugustin Cavalier			;;
416a5c952dbSAugustin Cavalier		darwin)
417a5c952dbSAugustin Cavalier			xattr_set="xattr"; xattr_set_args="-w \$NAME \"\$VALUE\""
418a5c952dbSAugustin Cavalier			xattr_get="xattr"; xattr_get_args="-p \$NAME"
419a5c952dbSAugustin Cavalier			;;
420a5c952dbSAugustin Cavalier		freebsd)
421a5c952dbSAugustin Cavalier			xattr_set="setextattr"; xattr_set_args="user \$NAME \"\$VALUE\""
422a5c952dbSAugustin Cavalier			xattr_get="getextattr"; xattr_get_args="user \$NAME"
423a5c952dbSAugustin Cavalier			;;
424a5c952dbSAugustin Cavalier		linux)
425a5c952dbSAugustin Cavalier			xattr_set="setfattr"; xattr_set_args="-n user.\$NAME -v \"\$VALUE\""
426a5c952dbSAugustin Cavalier			xattr_get="getfattr"; xattr_get_args="-n user.\$NAME"
427a5c952dbSAugustin Cavalier			;;
428a5c952dbSAugustin Cavalier		*)
429a5c952dbSAugustin Cavalier			return 0
430a5c952dbSAugustin Cavalier			;;
431a5c952dbSAugustin Cavalier	esac
432a5c952dbSAugustin Cavalier	if ! type $xattr_set >/dev/null 2>&1; then
433a5c952dbSAugustin Cavalier		echo "$0: could not find $xattr_set, assuming host has no extended attributes"
434a5c952dbSAugustin Cavalier		return 0
435a5c952dbSAugustin Cavalier	elif ! type $xattr_get >/dev/null 2>&1; then
436a5c952dbSAugustin Cavalier		echo "$0: could not find $xattr_get, assuming host has no extended attributes"
437a5c952dbSAugustin Cavalier		return 0
438a5c952dbSAugustin Cavalier	fi
439a5c952dbSAugustin Cavalier
44043f5f35aSAugustin Cavalier	mkdir -p "$outputDir"
441a5c952dbSAugustin Cavalier	echo "xattr test file" >"$outputDir/xattrtest"
442a5c952dbSAugustin Cavalier	local i=0
443a5c952dbSAugustin Cavalier	# on round 0, we test if we can set 3 attrs of 1K each (enough for xattr-ref)
444a5c952dbSAugustin Cavalier	# on round 1, we test if we can set 3 attrs of 45K each (enough for full xattr)
445a5c952dbSAugustin Cavalier	while [ $i -lt 2 ]; do
446a5c952dbSAugustin Cavalier		local j=0
447a5c952dbSAugustin Cavalier		while [ $j -lt 3 ]; do
448a5c952dbSAugustin Cavalier			NAME=attr$j
449a5c952dbSAugustin Cavalier			VALUE=`printf '%*s' $((1024 + $i * 45056)) "" | tr ' ' x`
450a5c952dbSAugustin Cavalier			if [ `echo -n $VALUE | wc -c` -lt $((1024 + $i * 45056)) ]; then
451a5c952dbSAugustin Cavalier				echo "$0: warning: could not generate test data for extended attributes"
452a5c952dbSAugustin Cavalier				rm "$outputDir/xattrtest"
453a5c952dbSAugustin Cavalier				return $i
454a5c952dbSAugustin Cavalier			elif ! $xattr_set `eval echo \"$xattr_set_args\"` \
455a5c952dbSAugustin Cavalier					"$outputDir/xattrtest" >/dev/null 2>&1 ; then
456a5c952dbSAugustin Cavalier				rm "$outputDir/xattrtest"
457a5c952dbSAugustin Cavalier				return $i
458a5c952dbSAugustin Cavalier			fi
459a5c952dbSAugustin Cavalier			j=$((j+1))
460a5c952dbSAugustin Cavalier		done
461a5c952dbSAugustin Cavalier		i=$((i+1))
462a5c952dbSAugustin Cavalier	done
463a5c952dbSAugustin Cavalier	rm "$outputDir/xattrtest"
464a5c952dbSAugustin Cavalier	return 2
465a5c952dbSAugustin Cavalier}
466a5c952dbSAugustin Cavalier
467483b28e4SIngo Weinholdis_in_list()
468483b28e4SIngo Weinhold{
469483b28e4SIngo Weinhold	local element
470483b28e4SIngo Weinhold	for element in $2; do
471483b28e4SIngo Weinhold		if [ "$1" = "$element" ]; then
472483b28e4SIngo Weinhold			return 0
473483b28e4SIngo Weinhold		fi
474483b28e4SIngo Weinhold	done
475483b28e4SIngo Weinhold	return 1
476483b28e4SIngo Weinhold}
477483b28e4SIngo Weinhold
4787f9beaf1SOliver Tappe# check for --help or -h and show usage immediately
4797f9beaf1SOliver Tappeif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
4808b420c2fSOliver Tappe	usage; exit 0;
4818b420c2fSOliver Tappefi
4828b420c2fSOliver Tappe
483338b8dc3SIngo Weinhold# get cwd and the source directory
484338b8dc3SIngo WeinholdcurrentDir=`pwd`
4856e7c6fe5SIngo Weinholdcd `dirname "$0"`
486338b8dc3SIngo WeinholdsourceDir=`pwd`
4876e7c6fe5SIngo Weinholdcd "$currentDir"
488338b8dc3SIngo Weinhold
489a107d3b4SAugustin Cavalier# determine output directory
490a107d3b4SAugustin Cavalierif [ "$currentDir" = "$sourceDir" ]; then
491a107d3b4SAugustin Cavalier	outputDir=$currentDir/generated
492a107d3b4SAugustin Cavalierelse
493a107d3b4SAugustin Cavalier	outputDir=$currentDir
494a107d3b4SAugustin Cavalierfi
495a107d3b4SAugustin CavalierbuildOutputDir="$outputDir/build"
496a107d3b4SAugustin CavalierHAIKU_BUILD_ATTRIBUTES_DIR="$outputDir/attributes"
497a107d3b4SAugustin CavalierbuildConfigFile="$buildOutputDir/BuildConfig"
498a107d3b4SAugustin Cavalier
499a107d3b4SAugustin Cavalier# check for update request
500a107d3b4SAugustin Cavalierif [ "$1" = "--update" ]; then
501a107d3b4SAugustin Cavalier	if ! [ -e "$buildConfigFile" ]; then
502a107d3b4SAugustin Cavalier		echo $0 --update: \'$buildConfigFile\' not found - updating not possible.
503a107d3b4SAugustin Cavalier		exit 1
504a107d3b4SAugustin Cavalier	fi
505a107d3b4SAugustin Cavalier	# get last configure invocation and flags from BuildConfig and call ourselves with it
506a107d3b4SAugustin Cavalier	lastPwd=`grep "#d " "$buildConfigFile" | cut -c 4-`
507a107d3b4SAugustin Cavalier	lastConfig=`grep "#c " "$buildConfigFile" | cut -c 4-`
508a107d3b4SAugustin Cavalier	lastEnv=`grep "#e " "$buildConfigFile" | cut -c 4-`
509a107d3b4SAugustin Cavalier	lastArgs=`grep "#a " "$buildConfigFile" | cut -c 4-`
510a107d3b4SAugustin Cavalier	if [ -z "$lastConfig" ]; then
511a107d3b4SAugustin Cavalier		echo "$0 --update: The previous configure invocation was not properly" \
512a107d3b4SAugustin Cavalier			"encoded into '$buildConfigFile' - updating not possible."
513a107d3b4SAugustin Cavalier		exit 1
514a107d3b4SAugustin Cavalier	fi
515a107d3b4SAugustin Cavalier	cd $lastPwd
516a107d3b4SAugustin Cavalier	if [ -n "$lastEnv" ]; then
517a107d3b4SAugustin Cavalier		export $lastEnv
518a107d3b4SAugustin Cavalier	fi
519a107d3b4SAugustin Cavalier	$lastConfig $lastArgs
520a107d3b4SAugustin Cavalier	exit $?
521a107d3b4SAugustin Cavalierfi
522a107d3b4SAugustin Cavalier
52355d267a4SFrançois Revol# backup the passed arguments
52455d267a4SFrançois RevolconfigureArgs="$@"
525825700d3SAugustin CavalierconfigurePath=$0
526825700d3SAugustin Cavalier
527825700d3SAugustin Cavalier# backup relevant environs
528825700d3SAugustin CavalierconfigureEnvirons=
529825700d3SAugustin Cavalierfor var in `env`; do
530825700d3SAugustin Cavalier	case "$var" in
531825700d3SAugustin Cavalier		CC\=*|HAIKU*\=*)
532825700d3SAugustin Cavalier			configureEnvirons="$configureEnvirons $var"
533825700d3SAugustin Cavalier			;;
534825700d3SAugustin Cavalier	esac
535825700d3SAugustin Cavalierdone
53655d267a4SFrançois Revol
537a107d3b4SAugustin Cavalier# ensure umask is not too restrictive
538a107d3b4SAugustin Cavalierif [ $(umask) -gt 22 ]; then
539