xref: /haiku/configure (revision c0936b5a0384bc6fe654d296ee54222a0f45d2b6)
1#!/bin/sh
2#
3# configure [ <options> ]
4
5# usage
6#
7# Prints usage.
8#
9usage()
10{
11	cat << EOF
12
13Usage: $0 <options>
14options:
15  --help                      Prints out this help.
16  --update                    Re-runs last configure invocation [must be given
17                              as first option!]
18  --bootstrap <haikuporter> <HaikuPorts cross repo> <HaikuPorts repo>
19                              Prepare for a bootstrap build. No pre-built
20                              packages will be used, instead they will be built
21                              from the sources (in several phases).
22                              <haikuporter> is the path to the haikuporter tool
23                              suitable for the host platform.
24                              <HaikuPorts cross repo> is the path to a checked
25                              out HaikuPorts cross-compilation repository.
26                              <HaikuPorts repo> is the path to a checked out
27                              HaikuPorts repository.
28  --build-cross-tools <arch> [ <build tools dir> ]
29                              Assume cross compilation. <build tools dir>
30                              defines the location of the build tools sources.
31                              They will be compiled and placed in the output
32                              directory under "cross-tools". The HAIKU_* tools
33                              variables will be set accordingly.
34                              <arch> specifies the target architecture, either
35                              "x86_gcc2", "x86", "x86_64", "ppc", "m68k", "arm"
36                              This option and --cross-tools-prefix can be
37                              specified multiple times. The first cross tools
38                              specify the primary tools, the subsequent ones the
39                              secondary tools (for "hybrid" images).
40                              For the first --build-cross-tools the
41                              <build tools dir> argument must be specified and
42                              for the subsequent ones it must be omitted.
43  --cross-tools-prefix <prefix>
44                              Assume cross compilation. <prefix> should be a
45                              path to the directory where the cross
46                              compilation tools are located, plus the platform
47                              prefix, e.g. "/path/to/tools/i586-pc-haiku-".
48                              This overrides the HAIKU_* tool variables.
49  --distro-compatibility <level>
50                              The distribution's level of compatibility with
51                              the official Haiku distribution. The generated
52                              files will contain the respective trademarks
53                              accordingly.
54                              official -- the official Haiku distribution.
55                              compatible -- a Haiku Compatible (tm) distro.
56                              default -- any other distro (default value).
57  --host-only                 Configure for building tools for the build host
58                              only. Haiku cannot be built when configured like
59                              this.
60  --include-patented-code     Enable code that is known to implemented patented
61                              ideas and techniques. If this option is not
62                              specified, the resulting distribution may still
63                              implement patented ideas and techniques. This
64                              option only enables code that is currently known
65                              to be problematic.
66  --include-sources           Includes the source code of projects that require
67                              either an offer of source code or a copy of the
68                              patched sources. This is preferable when
69                              distributing on physical mediums.
70  --include-3rdparty          Include 3rdparty/ in the build system.
71  -j<n>                       Only relevant for --build-cross-tools. Is passed
72                              on to the make building the build tools.
73  --no-downloads              Do not download anything. Useful when trying to
74                              bootstrap and build Haiku from source only.
75  --target-arch <arch>        Haiku only: Specify the target architecture to
76                              build for. Must be one of the architectures of the
77                              host system. The installed build tools for that
78                              architecture will be used.
79                              This option can be specified multiple times. The
80                              first occurrence specifies the primary
81                              architecture of the Haiku to build, subsequent
82                              ones the secondary architectures.
83  --use-clang <arch>          Build with host Clang instead of GCC cross
84                              compiler, targeting <arch>
85  --use-gcc-pipe              Build with GCC option -pipe. Speeds up the build
86                              process, but uses more memory.
87  --use-gcc-graphite          Build with GCC Graphite engine for loop
88                              optimizations. (Only for GCC 4+.)
89  --use-32bit                 Use -m32 flag on 64bit host gcc compiler.
90  --no-full-xattr             Do not use Linux/*BSD/Darwin's native extended file
91                              attributes as Haiku attributes. If they are still
92                              available, they will be used to store hashes for
93                              the attribute emulation layer.
94  --no-xattr                  Do not use Linux/*BSD/Darwin's native extended file
95                              attributes for Haiku extended attributes at all,
96                              even if they are available.
97  --with-gdb <gdb sources dir>
98                              specify the path to a GDB source dir, to build
99                              GDB for each arch we build the cross-tools for.
100
101environment variables:
102  CC                          The host compiler. Defaults to "gcc".
103  HAIKU_AR_<arch>             The static library archiver for <arch>.
104                              Defaults to "ar".
105  HAIKU_CC_<arch>             The compiler for <arch>. Defaults to "gcc".
106  HAIKU_LD_<arch>             The <arch> linker. Defaults to "ld".
107  HAIKU_OBJCOPY_<arch>        The <arch> objcopy to be used. Defaults to
108                              "objcopy".
109  HAIKU_RANLIB_<arch>         The static library indexer for <arch>. Defaults
110                              to "ranlib".
111  HAIKU_STRIP_<arch>          The <arch> strip command. Defaults to "strip".
112  HAIKU_NASM                  The nasm assembler (x86 and x86_64 only).
113  HAIKU_CPPFLAGS_<arch>       The preprocessor flags for target architecture
114                              <arch>. Defaults to "".
115  HAIKU_CCFLAGS_<arch>        The C flags for target architecture <arch>.
116                              Defaults to "".
117  HAIKU_CXXFLAGS_<arch>       The C++ flags for target architecture <arch>.
118                              Defaults to "".
119  HAIKU_LDFLAGS_<arch>        The linker flags for target architecture <arch>.
120                              Defaults to "".
121  HAIKU_ARFLAGS_<arch>        The flags passed to HAIKU_AR for target
122                              architecture <arch> for archiving. Defaults to
123                              "cru".
124  HAIKU_UNARFLAGS_<arch>      The flags passed to HAIKU_AR for target
125                              architecture <arch> for unarchiving. Defaults to
126                              "x".
127
128Non-default output directories:
129  By default all objects, build configuration, and other related files are
130  stored in /path/to/haiku_source/generated.  To store objects in a non-default
131  location, run "../../relative/path/to/haiku_source/configure <options>" from
132  within your non-default location.  "jam [ options ] targets" can then be run
133  directly inside your non-default location.  Another option is to invoke "jam
134  [ options ] targets" from within haiku_source.  This can be accomplished by
135  either "export HAIKU_OUTPUT_DIR=your non-default location" before invoking
136  jam or by creating a symlink of haiku_source/generated pointing to your
137  non-default location and running jam.
138
139
140EOF
141}
142
143# assertparam
144#
145# Checks whether at least one parameter is left.
146#
147assertparam()
148{
149	if [ $2 -lt 2 ]; then
150		echo $0: \`$1\': Parameter expected.
151		exit 1
152	fi
153}
154
155# assertparams
156#
157# Checks whether at least a certain number of parameters is left.
158#
159assertparams()
160{
161	if [ $3 -le $2 ]; then
162		echo $0: \`$1\': Not enough parameters.
163		exit 1
164	fi
165}
166
167# absolute_path
168#
169# returns the absolute path of a given path.
170#
171absolute_path()
172{
173	if [ "x$1" != "x${1#/}" ]; then
174		echo "$1"
175	else
176		echo "`pwd`/$1"
177	fi
178}
179
180# check_dir_exists
181#
182# check if a directory exists or not
183#
184check_dir_exists()
185{
186	if [ -d "$1" ]; then
187		return 0
188	else
189		return 1
190	fi
191}
192
193# check_file_exists
194#
195# check if a file exists or not
196#
197check_file_exists()
198{
199	if [ -f "$1" ]; then
200		return 0
201	else
202		return 1
203	fi
204}
205
206# real_path
207#
208# returns the realpath of a symbolic link.
209#
210real_path()
211{
212	perl -MCwd=realpath -e'print realpath($ARGV[0]), "\n"' "$1"
213}
214
215# standard_gcc_settings
216#
217# Sets the variables for a GCC platform.
218#
219standard_gcc_settings()
220{
221	local gcc="$1"
222
223	if which greadlink > /dev/null 2>&1; then
224		readlink="greadlink -e"
225	elif which realpath > /dev/null 2>&1; then
226		readlink=realpath
227	elif readlink -e / > /dev/null 2>&1; then
228		readlink="readlink -e"
229	else
230		readlink=real_path
231	fi
232
233	# PLATFORM_LINKLIBS
234	local gcclib=`$gcc -print-libgcc-file-name`
235	local gccdir=`dirname ${gcclib}`
236
237	local gccRawVersion=`$gcc -dumpversion`
238	local gccMachine=`$gcc -dumpmachine`
239
240	# determine architecture from machine triple
241	case $gccMachine in
242		arm-*)		targetCpu=arm;;
243		i?86-*)		targetCpu=x86;;
244		m68k-*)		targetCpu=m68k;;
245		powerpc-*)	targetCpu=ppc;;
246		x86_64-*)	targetCpu=x86_64;;
247		*)
248			echo "Unsupported gcc target machine: $gccMachine" >&2
249			exit 1
250			;;
251	esac
252
253	local targetArch=$targetCpu
254
255	case $gccRawVersion in
256		2.9*)
257			# check for correct (most up-to-date) legacy compiler and complain
258			# if an older one is installed
259			if [ $gccRawVersion != $haikuRequiredLegacyGCCVersion ]; then
260				echo "GCC version $haikuRequiredLegacyGCCVersion is required!";
261				echo "Please download it from www.haiku-os.org...";
262				exit 1;
263			fi
264
265			targetArch=x86_gcc2
266			;;
267	esac
268
269	local bootLibgcc
270	local bootLibSupCxx
271	local bootCxxHeaders
272	case $gccMachine in
273		x86_64-*)
274			# Boot loader is 32-bit, need the 32-bit libs and c++ config
275			bootLibgcc=`$gcc -m32 -print-file-name=libgcc.a`
276			bootLibSupCxx=`$gcc -m32 -print-file-name=libsupc++.a`
277
278			local headersBase=$gccdir/../../../..
279			local headers=$headersBase/$gccMachine/include/c++/$gccRawVersion
280			if [ ! -d $headers ]; then
281				headers=$headersBase/include/c++/$gccRawVersion
282			fi
283			bootCxxHeaders="$headers/$gccMachine/32"
284			;;
285	esac
286
287	# determine whether graphite loop optimization should/can be used
288	local useGraphite=`get_variable HAIKU_USE_GCC_GRAPHITE_$targetCpu`
289	if [ -z "$useGraphite" ]; then
290		useGraphite=$useGccGraphiteDefault
291	fi
292
293	if [ "$useGraphite" != 0 ]; then
294		UNUSED=`echo "int main() {}" | $gcc -xc -c -floop-block - 2>&1`
295		if [ $? != 0 ]; then
296			echo "GCC Graphite loop optimizations cannot be used on $targetArch"
297			useGraphite=0
298		fi
299	fi
300
301	set_variable HAIKU_CPU_$targetArch $targetCpu
302
303	get_build_tool_path CC_$targetArch "$gcc"
304	set_variable HAIKU_CC_IS_CLANG_$targetArch $useClang
305	set_variable HAIKU_GCC_RAW_VERSION_$targetArch $gccRawVersion
306	set_variable HAIKU_GCC_MACHINE_$targetArch $gccMachine
307	set_variable HAIKU_GCC_LIB_DIR_$targetArch $gccdir
308	set_variable HAIKU_BOOT_CXX_HEADERS_DIR_$targetArch "$bootCxxHeaders"
309	set_variable HAIKU_BOOT_LIBSUPCXX_$targetArch "$bootLibSupCxx"
310	set_variable HAIKU_BOOT_LIBGCC_$targetArch $bootLibgcc
311	set_variable HAIKU_USE_GCC_GRAPHITE_$targetArch $useGraphite
312
313	standard_gcc_settings_targetArch=$targetArch
314}
315
316# set_variable
317#
318# Set the value of a variable.
319#
320set_variable()
321{
322	eval "$1=\"$2\""
323}
324
325# get_variable
326#
327# Echo the value of a variable.
328#
329get_variable()
330{
331	eval "echo \${$1}"
332}
333
334# set_default_value
335#
336# Set the value for a variable, if no value is set yet.
337#
338set_default_value()
339{
340	eval "$1=\${$1-$2}"
341}
342
343# get_build_tool_path
344#
345# Gets a usable absolute path of a build tool.
346#
347get_build_tool_path()
348{
349	local var="HAIKU_$1"
350	local varval="`get_variable $var`"
351	local cmd="$2"
352
353	if [ ! -z "$varval" ]; then
354		# this variable is already set (probably by user) so grab its contents
355		cmd=$varval
356	fi
357
358	local path=${cmd%% *}
359
360	if [ -f "$path" ]; then
361		# get absolute path from relative path
362		local oldPwd="`pwd`"
363		cd "`dirname "$path"`"
364		path="`pwd`/`basename "$path"`"
365		cd $oldPwd
366	else
367		which "$path" > /dev/null 2>&1 || {
368			echo "Build tool \"$path\" not found (maybe specify it in $var?)" >&2
369			exit 1
370		}
371	fi
372
373	if test "${cmd#* }" != "$cmd"; then
374		# $cmd contains arguments, so preserve them (and only them)
375		cmd=${cmd#* }
376	else
377		# $cmd does not contain arguments, so unset it
378		cmd=
379	fi
380	eval "$var=\"$path $cmd\""
381}
382
383# check_native_xattrs
384#
385# Checks the host platform's support for extended attributes.
386# 0: no support, 1: only enough for xattr-ref, 2: full support
387#
388check_native_xattrs()
389{
390	local xattr_set=
391	local xattr_set_args=
392	local xattr_get=
393	local xattr_get_args=
394	case $HOST_PLATFORM in
395		haiku_host)
396			xattr_set="addattr"; xattr_set_args="\$NAME \"\$VALUE\""
397			xattr_get="catattr"; xattr_get_args="\$NAME"
398			;;
399		darwin)
400			xattr_set="xattr"; xattr_set_args="-w \$NAME \"\$VALUE\""
401			xattr_get="xattr"; xattr_get_args="-p \$NAME"
402			;;
403		freebsd)
404			xattr_set="setextattr"; xattr_set_args="user \$NAME \"\$VALUE\""
405			xattr_get="getextattr"; xattr_get_args="user \$NAME"
406			;;
407		linux)
408			xattr_set="setfattr"; xattr_set_args="-n user.\$NAME -v \"\$VALUE\""
409			xattr_get="getfattr"; xattr_get_args="-n user.\$NAME"
410			;;
411		*)
412			return 0
413			;;
414	esac
415	if ! type $xattr_set >/dev/null 2>&1; then
416		echo "$0: could not find $xattr_set, assuming host has no extended attributes"
417		return 0
418	elif ! type $xattr_get >/dev/null 2>&1; then
419		echo "$0: could not find $xattr_get, assuming host has no extended attributes"
420		return 0
421	fi
422
423	echo "xattr test file" >"$outputDir/xattrtest"
424	local i=0
425	# on round 0, we test if we can set 3 attrs of 1K each (enough for xattr-ref)
426	# on round 1, we test if we can set 3 attrs of 45K each (enough for full xattr)
427	while [ $i -lt 2 ]; do
428		local j=0
429		while [ $j -lt 3 ]; do
430			NAME=attr$j
431			VALUE=`printf '%*s' $((1024 + $i * 45056)) "" | tr ' ' x`
432			if [ `echo -n $VALUE | wc -c` -lt $((1024 + $i * 45056)) ]; then
433				echo "$0: warning: could not generate test data for extended attributes"
434				rm "$outputDir/xattrtest"
435				return $i
436			elif ! $xattr_set `eval echo \"$xattr_set_args\"` \
437					"$outputDir/xattrtest" >/dev/null 2>&1 ; then
438				rm "$outputDir/xattrtest"
439				return $i
440			fi
441			j=$((j+1))
442		done
443		i=$((i+1))
444	done
445	rm "$outputDir/xattrtest"
446	return 2
447}
448
449is_in_list()
450{
451	local element
452	for element in $2; do
453		if [ "$1" = "$element" ]; then
454			return 0
455		fi
456	done
457	return 1
458}
459
460# check for --help or -h and show usage immediately
461if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
462	usage; exit 0;
463fi
464
465# ensure umask is not too restrictive
466if [ $(umask) -gt 22 ]; then
467	echo "Your umask is too restrictive (should be: <= 0022; is actually:" $(umask)")"
468	exit 1
469fi
470
471# get cwd and the source directory
472currentDir=`pwd`
473cd `dirname "$0"`
474sourceDir=`pwd`
475cd "$currentDir"
476
477# backup the passed arguments
478configureArgs="$@"
479configurePath=$0
480
481# backup relevant environs
482configureEnvirons=
483for var in `env`; do
484	case "$var" in
485		CC\=*|HAIKU*\=*)
486			configureEnvirons="$configureEnvirons $var"
487			;;
488	esac
489done
490
491# internal default parameter values
492#
493platform=`uname`
494platformMachine=`uname  -m`
495targetArchs=
496buildCrossTools=
497buildCrossToolsScript="$sourceDir/build/scripts/build_cross_tools"
498buildCrossToolsJobs=
499useClang=0
500useGccGraphiteDefault=0
501unknownArchIndex=1
502haikuTargetArchs=
503gdbSources=
504
505if [ -z "$CC" ]; then
506	CC=gcc
507fi
508
509# exported (BuildSetup) default parameter values
510#
511HOST_GCC_RAW_VERSION=`$CC -dumpversion`
512HOST_GCC_MACHINE=`$CC -dumpmachine`
513HAIKU_INCLUDE_PATENTED_CODE=0
514HAIKU_INCLUDE_SOURCES=0
515HAIKU_INCLUDE_3RDPARTY=0
516HAIKU_DISTRO_COMPATIBILITY=default
517TARGET_PLATFORM=haiku
518HAIKU_USE_GCC_PIPE=0
519HAIKU_HOST_USE_32BIT=0
520HAIKU_HOST_USE_XATTR=
521HAIKU_HOST_USE_XATTR_REF=
522HAIKU_HOST_BUILD_ONLY=0
523HOST_EXTENDED_REGEX_SED="sed -r"
524HOST_GCC_LD=`$CC -print-prog-name=ld`
525HOST_GCC_OBJCOPY=`$CC -print-prog-name=objcopy`
526HOST_SHA256=
527HOST_HAIKU_PORTER=
528HAIKU_PORTS=
529HAIKU_PORTS_CROSS=
530HAIKU_IS_BOOTSTRAP=0
531HAIKU_BOOT_BOARD=
532HAIKU_NO_DOWNLOADS=0
533
534HAIKU_PACKAGING_ARCHS=
535
536set_default_value HAIKU_NASM		nasm
537
538if sha256sum < /dev/null > /dev/null 2>&1; then
539	HOST_SHA256=sha256sum
540elif sha256 < /dev/null > /dev/null 2>&1; then
541	HOST_SHA256="sha256 -q"
542elif shasum < /dev/null > /dev/null 2>&1; then
543	HOST_SHA256="shasum -a 256"
544else
545	echo "Error: Neither sha256sum nor sha256 seem to be available!" >&2
546	exit 1
547fi
548
549haikuRequiredLegacyGCCVersion="2.95.3-haiku-2017_07_20"
550export haikuRequiredLegacyGCCVersion
551	# version of legacy gcc required to build haiku
552supportedTargetArchs="
553	arm
554	m68k
555	ppc
556	x86
557	x86_64
558	x86_gcc2
559	"
560
561# determine output directory
562if [ "$currentDir" = "$sourceDir" ]; then
563	outputDir=$currentDir/generated
564else
565	outputDir=$currentDir
566fi
567buildOutputDir="$outputDir/build"
568HAIKU_BUILD_ATTRIBUTES_DIR="$outputDir/attributes"
569buildConfigFile="$buildOutputDir/BuildConfig"
570
571# check for update request
572if [ "$1" = "--update" ]; then
573	if ! [ -e "$buildConfigFile" ]; then
574		echo $0 --update: \'$buildConfigFile\' not found - updating not possible.
575		exit 1
576	fi
577	# get last configure invocation and flags from BuildConfig and call ourselves with it
578	lastPwd=`grep "#d " "$buildConfigFile" | cut -c 4-`
579	lastConfig=`grep "#c " "$buildConfigFile" | cut -c 4-`
580	lastEnv=`grep "#e " "$buildConfigFile" | cut -c 4-`
581	lastArgs=`grep "#a " "$buildConfigFile" | cut -c 4-`
582	if [ -z "$lastConfig" ]; then
583		echo "$0 --update: The previous configure invocation was not properly" \
584			"encoded into '$buildConfigFile' - updating not possible."
585		exit 1
586	fi
587	cd $lastPwd
588	if [ -n "$lastEnv" ]; then
589		export $lastEnv
590	fi
591	$lastConfig $lastArgs
592	exit $?
593fi
594
595# parse parameters
596#
597while [ $# -gt 0 ] ; do
598	case "$1" in
599		--bootstrap)
600			assertparams "$1" 3 $#
601			HOST_HAIKU_PORTER="`absolute_path $2`"
602			HAIKU_PORTS_CROSS="`absolute_path $3`"
603			HAIKU_PORTS="`absolute_path $4`"
604			HAIKU_IS_BOOTSTRAP=1
605			HAIKU_NO_DOWNLOADS=1
606			check_file_exists "$HOST_HAIKU_PORTER" || (
607				echo "Invalid path to haikuporter: $HOST_HAIKU_PORTER" >&2
608				exit 1
609			)
610			check_dir_exists "$HAIKU_PORTS" || (
611				echo "Non-existent directory $HAIKU_PORTS" >&2
612				exit 1
613			)
614			check_dir_exists "$HAIKU_PORTS_CROSS" || (
615				echo "Non-existent directory $HAIKU_PORTS_CROSS" >&2
616				exit 1
617			)
618			shift 4
619			;;
620		--build-cross-tools)
621			if [ -z "$buildCrossTools" ]; then
622				assertparams "$1" 2 $#
623				targetArch=$2
624				buildCrossTools=$3
625				shift 3
626			else
627				assertparam "$1" $#
628				targetArch=$2
629				shift 2
630			fi
631			case "$targetArch" in
632				x86_gcc2)	targetMachine=i586-pc-haiku;;
633				x86)		targetMachine=i586-pc-haiku;;
634				x86_64)		targetMachine=x86_64-unknown-haiku;;
635				ppc)		targetMachine=powerpc-apple-haiku;;
636				m68k)		targetMachine=m68k-unknown-haiku;;
637				arm)		targetMachine=arm-unknown-haiku;;
638				*)
639					echo "Unsupported target architecture: $2" >&2
640					exit 1
641					;;
642			esac
643			set_variable buildCrossToolsMachine_$targetArch $targetMachine
644			targetArchs="$targetArchs $targetArch"
645			HAIKU_PACKAGING_ARCHS=
646			;;
647		--cross-tools-prefix)
648			assertparam "$1" $#
649			targetArch=unknown${unknownArchIndex}
650			set_variable crossToolsPrefix_$targetArch "$2"
651			targetArchs="$targetArchs $targetArch"
652			HAIKU_PACKAGING_ARCHS=
653			unknownArchIndex=$(($unknownArchIndex + 1))
654			shift 2
655			;;
656		--distro-compatibility)
657			assertparam "$1" $#
658			HAIKU_DISTRO_COMPATIBILITY=$2
659			case "$HAIKU_DISTRO_COMPATIBILITY" in
660				official)	;;
661				compatible)	;;
662				default)	;;
663				*)			echo "Invalid distro compatibility" \
664								"level: $HAIKU_DISTRO_COMPATIBILITY"
665							exit 1;;
666			esac
667			shift 2
668			;;
669		--host-only)	HAIKU_HOST_BUILD_ONLY=1; shift 1;;
670		--include-patented-code)	HAIKU_INCLUDE_PATENTED_CODE=1; shift 1;;
671		--include-sources)	HAIKU_INCLUDE_SOURCES=1; shift 1;;
672		--include-3rdparty)	HAIKU_INCLUDE_3RDPARTY=1; shift 1;;
673        -j*)				buildCrossToolsJobs="$1"; shift 1;;
674		--no-downloads)	HAIKU_NO_DOWNLOADS=1; shift 1;;
675		--target-arch)
676			assertparam "$1" $#
677			targetArch=$2
678			shift 2
679			if [ ! "$platform" = Haiku ]; then
680				echo "--target-arch can only be specified on Haiku." >&2
681				exit 1
682			fi
683			is_in_list "$targetArch" "$supportedTargetArchs" || (
684				echo "Unsupported target architecture: \"$targetArch\"" >&2
685				exit 1
686			)
687			haikuTargetArchs="$haikuTargetArchs $targetArch"
688			;;
689		--use-clang)
690			assertparam "$1" $#
691			targetArch=$2
692			useClang=1
693			case "$targetArch" in
694				x86)		targetMachine=i586-pc-haiku;;
695				x86_64)		targetMachine=x86_64-unknown-haiku;;
696				*)
697					echo "Unsupported target architecture: $2" >&2
698					exit 1
699					;;
700			esac
701			get_build_tool_path clang clang
702			if [ -z `get_variable "crossToolsPrefix_$targetArch"` ] \
703					&& [ -z `get_variable buildCrossToolsMachine_$targetArch` ]; then
704				set_variable crossToolsPrefix_$targetArch llvm-
705			fi
706			if ! test "${targetArchs#*$targetArch}" != "$targetArchs"; then
707				# we have not already added this arch to targetArchs, so add it now
708				targetArchs="$targetArchs $targetArch"
709			fi
710			HAIKU_PACKAGING_ARCHS=
711			shift 2
712			;;
713		--use-gcc-pipe)	HAIKU_USE_GCC_PIPE=1; shift 1;;
714		--use-gcc-graphite)	useGccGraphiteDefault=1; shift 1;;
715		--use-32bit)	HAIKU_HOST_USE_32BIT=1; shift 1;;
716		--no-full-xattr)HAIKU_HOST_USE_XATTR=0; shift 1;;
717		--no-xattr)		HAIKU_HOST_USE_XATTR_REF=0; shift 1;;
718		--with-gdb)	gdbSources=$2; shift 2;;
719		*)				echo Invalid argument: \`$1\'; exit 1;;
720	esac
721done
722
723# detect the build platform
724case "${platform}" in
725	Darwin)	HOST_PLATFORM=darwin ;;
726	FreeBSD)	HOST_PLATFORM=freebsd
727				if [ "$HAIKU_HOST_USE_32BIT" = 1 ] ; then
728					echo Unsupported platform: FreeBSD ${platformMachine}
729					exit 1
730				fi	;;
731	Haiku)	HOST_PLATFORM=haiku_host ;;
732	Linux)	HOST_PLATFORM=linux ;;
733	OpenBSD) HOST_PLATFORM=openbsd ;;
734	SunOS)	HOST_PLATFORM=sunos ;;
735	CYGWIN_NT-*) HOST_PLATFORM=cygwin ;;
736	*)		echo Unsupported platform: ${platform}
737			exit 1 ;;
738esac
739
740# check for case-sensitive filesystem
741mkdir haikuCaseTest 2>/dev/null
742mkdir haikucasetest 2>/dev/null
743caseInsensitive=$?
744rmdir haikuCaseTest haikucasetest 2>/dev/null
745if [ $caseInsensitive != 0 ]; then
746	echo "You need a case-sensitive file-system to build Haiku."
747	if [ $HOST_PLATFORM = "darwin" ]; then
748		echo "You can create a case-sensitive disk image using Disk Utility."
749	fi
750	exit 1
751fi
752
753# check xattr support
754if [ -z $HAIKU_HOST_USE_XATTR_REF ]; then
755	check_native_xattrs
756	attrSupport=$?
757	if [ $attrSupport = 2 ] && [ -z $HAIKU_HOST_USE_XATTR ]; then
758		HAIKU_HOST_USE_XATTR=1
759	elif [ $attrSupport = 1 ]; then
760		HAIKU_HOST_USE_XATTR_REF=1
761	fi
762fi
763if [ -z $HAIKU_HOST_USE_XATTR ]; then HAIKU_HOST_USE_XATTR=0; fi
764if [ -z $HAIKU_HOST_USE_XATTR_REF ]; then HAIKU_HOST_USE_XATTR_REF=0; fi
765
766# determine how to invoke sed with extended regexp support for non-GNU sed
767if [ $HOST_PLATFORM = "darwin" ]; then
768	HOST_EXTENDED_REGEX_SED="sed -E"
769fi
770
771# check if nasm can actually output ELF files
772# (the stock version in OSX can't)
773# XXX: should probably only test for x86* arch
774if [ "$("$HAIKU_NASM" -hf | grep -c elf'[36][24] ')" -ne "2" ]; then
775	echo "$HAIKU_NASM cannot generate ELF files. Please install a working version."
776	if [ $HOST_PLATFORM = "darwin" ]; then
777		echo "You can install it from Mac Ports."
778		echo "Mac Ports is available at: http://www.macports.org/"
779	fi
780	exit 1
781fi
782
783# create output directory
784mkdir -p "$buildOutputDir" || exit 1
785
786if [ "$HAIKU_HOST_BUILD_ONLY" = 1 ]; then
787	invalidCommand=$sourceDir/build/scripts/host_build_only
788	HAIKU_AR=$invalidCommand
789	HAIKU_CC=$invalidCommand
790	HAIKU_LD=$invalidCommand
791	HAIKU_OBJCOPY=$invalidCommand
792	HAIKU_RANLIB=$invalidCommand
793	HAIKU_ELFEDIT=$invalidCommand
794	HAIKU_NASM=$invalidCommand
795	HAIKU_STRIP=$invalidCommand
796else
797	if [ -n "$HAIKU_PACKAGING_ARCHS" ]; then
798		targetArchs="$HAIKU_PACKAGING_ARCHS"
799	fi
800	HAIKU_PACKAGING_ARCHS=
801
802	# On Haiku determine target architectures and tools automatically.
803	if [ -z "$targetArchs" ]; then
804		if [ $HOST_PLATFORM != haiku_host ]; then
805			echo "Please specify the build tools to use or build (via" \
806				"--cross-tools-prefix or --build-cross-tools) or specify a" \
807				"host-only build (--host-only)." >&2
808			echo "For more info, invoke $0 --help"
809			exit 1
810		fi
811
812		# determine primary architecture
813		targetArch=`package list -i /system/packages/haiku-*.hpkg \
814			| sed '/^\s*architecture:/!d; s,^\s*architecture:\s*,,'`
815		is_in_list "$targetArch" "$supportedTargetArchs" || (
816			echo "Unsupported target architecture: \"$targetArch\"" >&2
817			exit 1
818		)
819		targetArchs=$targetArch
820
821		set_default_value HAIKU_AR_$targetArch			ar
822		set_default_value HAIKU_CC_$targetArch			gcc
823		set_default_value HAIKU_LD_$targetArch			ld
824		set_default_value HAIKU_OBJCOPY_$targetArch		objcopy
825		set_default_value HAIKU_RANLIB_$targetArch		ranlib
826		set_default_value HAIKU_ELFEDIT_$targetArch		elfedit
827		set_default_value HAIKU_STRIP_$targetArch		strip
828
829		# determine secondary architectures
830		for targetArch in $supportedTargetArchs; do
831			if [ -e /system/packages/haiku_$targetArch-*.hpkg ]; then
832				targetArchs="$targetArchs $targetArch"
833				set_default_value HAIKU_AR_$targetArch		ar-$targetArch
834				set_default_value HAIKU_CC_$targetArch		gcc-$targetArch
835				set_default_value HAIKU_LD_$targetArch		ld-$targetArch
836				set_default_value HAIKU_OBJCOPY_$targetArch	objcopy-$targetArch
837				set_default_value HAIKU_RANLIB_$targetArch	ranlib-$targetArch
838				set_default_value HAIKU_ELFEDIT_$targetArch	elfedit-$targetArch
839				set_default_value HAIKU_STRIP_$targetArch	strip-$targetArch
840			fi
841		done
842
843		# The target architectures might have been specified explicitly.
844		if [ -n "$haikuTargetArchs" ]; then
845			for targetArch in $haikuTargetArchs; do
846				is_in_list "$targetArch" "$targetArchs" || (
847					echo "Unsupported target architecture: \"$targetArch\"." \
848						"Only native architectures of the host platform can" \
849						"be specified." >&2
850					exit 1
851				)
852			done
853			targetArchs="$haikuTargetArchs"
854		fi
855	fi
856
857	isPrimaryArch=1
858	for targetArch in $targetArchs; do
859		# Note: targetArch is "unknown<n>" at this point, if a cross-tools
860		# prefix was specified. The standard_gcc_settings call below will get
861		# the actual architecture.
862
863		crossToolsPrefix=`get_variable crossToolsPrefix_$targetArch`
864
865		# build cross tools from sources
866		if [ -n "$buildCrossTools" -a -z "$crossToolsPrefix" ]; then
867			crossToolsDir="$outputDir/cross-tools-$targetArch"
868			targetMachine=`get_variable buildCrossToolsMachine_$targetArch`
869			script="$buildCrossToolsScript"
870			scriptArgs=
871			if [ $targetArch != x86_gcc2 ]; then
872				script="${script}_gcc4"
873				scriptArgs="$targetMachine"
874				set_default_value HAIKU_USE_GCC_GRAPHITE_$targetArch	\
875					$useGccGraphiteDefault
876			fi
877			secondaryArch=
878			if [ -z "$isPrimaryArch" ]; then
879				secondaryArch=$targetArch
880			fi
881
882			case $HOST_PLATFORM in
883				freebsd|openbsd)	MAKE=gmake;;
884				*)					MAKE=make;;
885			esac
886
887			if [ ! -d "$crossToolsDir" ]; then
888				MAKE=$MAKE \
889				SECONDARY_ARCH=$secondaryArch \
890				HAIKU_USE_GCC_GRAPHITE=`get_variable \
891					HAIKU_USE_GCC_GRAPHITE_$targetArch` \
892				HAIKU_USE_GCC_PIPE=$HAIKU_USE_GCC_PIPE \
893				HAIKU_USE_GDB="$gdbSources" \
894				"$script" $scriptArgs "$sourceDir" "$buildCrossTools" \
895					"$crossToolsDir" $buildCrossToolsJobs || exit 1
896			else
897				echo "$targetArch crosstools already exist in $crossToolsDir; skipping build"
898			fi
899			crossToolsPrefix="$crossToolsDir/bin/${targetMachine}-"
900		fi
901
902		# prepare gcc settings and get the actual target architecture
903		if [ $useClang = 1 ]; then
904			gcc="$HAIKU_clang -target ${targetMachine} -no-integrated-as"
905
906			# Clang's compiler intrinsics are not compatible with GCC's or even
907			# across versions of Clang, so we must collect them for use in the build.
908			mkdir -p "$outputDir/clang_headers" || exit 1
909			clangHeadersDir=`$gcc -print-resource-dir`/include/
910			cp $clangHeadersDir/*intrin* $clangHeadersDir/mm3* "$outputDir/clang_headers" || exit 1
911		elif [ -z "${crossToolsPrefix}" ]; then
912			gcc=`get_variable HAIKU_CC_$targetArch`
913		else
914			gcc="${crossToolsPrefix}gcc"
915		fi
916		standard_gcc_settings "$gcc"
917		targetArch=$standard_gcc_settings_targetArch
918
919		# set default values for flags
920		set_default_value HAIKU_CPPFLAGS_$targetArch	""
921		set_default_value HAIKU_CCFLAGS_$targetArch		""
922		set_default_value HAIKU_CXXFLAGS_$targetArch	""
923		set_default_value HAIKU_LDFLAGS_$targetArch		""
924		set_default_value HAIKU_ARFLAGS_$targetArch		cru
925		set_default_value HAIKU_UNARFLAGS_$targetArch	x
926
927		# Override the cross tools variables, if the tools were built or a
928		# prefix was specified.
929		if [ -n "$crossToolsPrefix" ]; then
930			get_build_tool_path AR_$targetArch ${crossToolsPrefix}ar
931			get_build_tool_path LD_$targetArch ${crossToolsPrefix}ld
932			get_build_tool_path OBJCOPY_$targetArch ${crossToolsPrefix}objcopy
933			get_build_tool_path RANLIB_$targetArch ${crossToolsPrefix}ranlib
934			get_build_tool_path STRIP_$targetArch ${crossToolsPrefix}strip
935
936			case `get_variable HAIKU_GCC_RAW_VERSION_$targetArch` in
937				4.*|5.*|6.*|7.*|8.*)
938					get_build_tool_path ELFEDIT_$targetArch \
939						${crossToolsPrefix}elfedit
940				;;
941			esac
942		fi
943
944		# check whether the Haiku compiler really targets Haiku
945		targetMachine=`get_variable HAIKU_GCC_MACHINE_$targetArch`
946		case "$targetMachine" in
947			*-*-haiku)	;;
948			*)
949				echo The compiler specified as Haiku target compiler is not a \
950				valid Haiku cross-compiler. Please see ReadMe.cross-compile. >&2
951				echo compiler: $HAIKU_CC
952				echo compiler is configured for target: $targetMachine
953				exit 1 ;;
954		esac
955
956		HAIKU_PACKAGING_ARCHS="$HAIKU_PACKAGING_ARCHS $targetArch"
957		isPrimaryArch=
958	done
959fi
960
961# Generate BuildConfig
962cat << EOF > "$buildConfigFile"
963# -- WARNING --
964# This file was AUTOMATICALLY GENERATED by configure, and will be completely
965# overwritten the next time configure is run.
966#
967#d ${currentDir}
968#c ${configurePath}
969#e ${configureEnvirons}
970#a ${configureArgs}
971
972TARGET_PLATFORM 			?= "${TARGET_PLATFORM}" ;
973HOST_PLATFORM				?= "${HOST_PLATFORM}" ;
974
975HAIKU_INCLUDE_PATENTED_CODE			?= "${HAIKU_INCLUDE_PATENTED_CODE}" ;
976HAIKU_INCLUDE_SOURCES				?= "${HAIKU_INCLUDE_SOURCES}" ;
977HAIKU_INCLUDE_3RDPARTY				?= "${HAIKU_INCLUDE_3RDPARTY}" ;
978HAIKU_DISTRO_COMPATIBILITY			?= "${HAIKU_DISTRO_COMPATIBILITY}" ;
979HAIKU_USE_GCC_PIPE					?= "${HAIKU_USE_GCC_PIPE}" ;
980HAIKU_HOST_USE_32BIT				?= "${HAIKU_HOST_USE_32BIT}" ;
981HAIKU_HOST_USE_XATTR				?= "${HAIKU_HOST_USE_XATTR}" ;
982HAIKU_HOST_USE_XATTR_REF			?= "${HAIKU_HOST_USE_XATTR_REF}" ;
983HAIKU_HOST_BUILD_ONLY				?= "${HAIKU_HOST_BUILD_ONLY}" ;
984
985HAIKU_PACKAGING_ARCHS		?= ${HAIKU_PACKAGING_ARCHS} ;
986
987HAIKU_NO_DOWNLOADS			?= "${HAIKU_NO_DOWNLOADS}" ;
988
989HAIKU_BUILD_ATTRIBUTES_DIR	?= ${HAIKU_BUILD_ATTRIBUTES_DIR} ;
990
991HAIKU_NASM					?= ${HAIKU_NASM} ;
992HAIKU_BOOT_BOARD			?= ${HAIKU_BOOT_BOARD} ;
993
994HOST_EXTENDED_REGEX_SED		?= ${HOST_EXTENDED_REGEX_SED} ;
995HOST_GCC_RAW_VERSION		?= ${HOST_GCC_RAW_VERSION} ;
996HOST_GCC_MACHINE			?= ${HOST_GCC_MACHINE} ;
997HOST_LD						?= ${HOST_GCC_LD} ;
998HOST_OBJCOPY				?= ${HOST_GCC_OBJCOPY} ;
999HOST_SHA256					?= ${HOST_SHA256} ;
1000
1001HOST_HAIKU_PORTER			?= ${HOST_HAIKU_PORTER} ;
1002HAIKU_PORTS					?= ${HAIKU_PORTS} ;
1003HAIKU_PORTS_CROSS			?= ${HAIKU_PORTS_CROSS} ;
1004HAIKU_IS_BOOTSTRAP			?= ${HAIKU_IS_BOOTSTRAP} ;
1005
1006EOF
1007
1008for targetArch in $HAIKU_PACKAGING_ARCHS; do
1009	variables="
1010		HAIKU_GCC_RAW_VERSION		HAIKU_GCC_RAW_VERSION
1011		HAIKU_GCC_MACHINE			HAIKU_GCC_MACHINE
1012		HAIKU_GCC_LIB_DIR			HAIKU_GCC_LIB_DIR
1013		HAIKU_CPU					HAIKU_CPU
1014		HAIKU_BOOT_LIBGCC			HAIKU_BOOT_LIBGCC
1015		HAIKU_BOOT_LIBSUPC++		HAIKU_BOOT_LIBSUPCXX
1016		HAIKU_AR					HAIKU_AR
1017		HAIKU_CC					HAIKU_CC
1018		HAIKU_CC_IS_CLANG			HAIKU_CC_IS_CLANG
1019		HAIKU_LD					HAIKU_LD
1020		HAIKU_OBJCOPY				HAIKU_OBJCOPY
1021		HAIKU_RANLIB				HAIKU_RANLIB
1022		HAIKU_ELFEDIT				HAIKU_ELFEDIT
1023		HAIKU_STRIP					HAIKU_STRIP
1024		HAIKU_CPPFLAGS				HAIKU_CPPFLAGS
1025		HAIKU_CCFLAGS				HAIKU_CCFLAGS
1026		HAIKU_C++FLAGS				HAIKU_CXXFLAGS
1027		HAIKU_LDFLAGS				HAIKU_LDFLAGS
1028		HAIKU_ARFLAGS				HAIKU_ARFLAGS
1029		HAIKU_UNARFLAGS				HAIKU_UNARFLAGS
1030		HAIKU_USE_GCC_GRAPHITE		HAIKU_USE_GCC_GRAPHITE
1031		"
1032	set -- $variables
1033	while [ $# -ge 2 ]; do
1034		value=`get_variable ${2}_$targetArch`
1035		echo "${1}_${targetArch} ?= $value ;" >> "$buildConfigFile"
1036		shift 2
1037	done
1038
1039	# For variables that may have long values, distribute them over multiple
1040	# lines so that jam doesn't hit the maximum line length.
1041	variables="
1042		HAIKU_BOOT_C++_HEADERS_DIR	HAIKU_BOOT_CXX_HEADERS_DIR
1043		"
1044	set -- $variables
1045	while [ $# -ge 2 ]; do
1046		echo "${1}_${targetArch} ?= " >> "$buildConfigFile"
1047		get_variable ${2}_$targetArch | xargs -n 1 echo "   " \
1048			>> "$buildConfigFile"
1049		echo "    ;" >> "$buildConfigFile"
1050		shift 2
1051	done
1052done
1053
1054
1055# Generate a boot strap Jamfile in the output directory.
1056
1057cat << EOF > $outputDir/Jamfile
1058# automatically generated Jamfile
1059
1060HAIKU_TOP			= ${sourceDir} ;
1061HAIKU_OUTPUT_DIR	= ${outputDir} ;
1062
1063include [ FDirName \$(HAIKU_TOP) Jamfile ] ;
1064
1065EOF
1066
1067echo "Configured successfully!"
1068