xref: /haiku/configure (revision 8304ec7c122b0c03abd84afb87386e35709cfe2d)
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  --bootstrap <haikuporter> <HaikuPorts cross repo> <HaikuPorts repo>
16                              Prepare for a bootstrap build. No pre-built
17                              packages will be used, instead they will be built
18                              from the sources (in several phases).
19                              <haikuporter> is the path to the haikuporter tool
20                              suitable for the host platform.
21                              <HaikuPorts cross repo> is the path to a checked
22                              out HaikuPorts cross-compilation repository.
23                              <HaikuPorts repo> is the path to a checked out
24                              HaikuPorts repository.
25  --build-cross-tools <arch> [ <build tools dir> ]
26                              Assume cross compilation. <build tools dir>
27                              defines the location of the build tools sources.
28                              They will be compiled and placed in the output
29                              directory under "cross-tools". The HAIKU_* tools
30                              variables will be set accordingly.
31                              <arch> specifies the target architecture, either
32                              "x86_gcc2", "x86", "x86_64", "ppc", "m68k", "arm",
33                              or "mipsel".
34                              This option and --cross-tools-prefix can be
35                              specified multiple times. The first cross tools
36                              specify the primary tools, the subsequent ones the
37                              secondary tools (for "hybrid" images).
38                              For the first --build-cross-tools the
39                              <build tools dir> argument must be specified and
40                              for the subsequent ones it must be omitted.
41  --cross-tools-prefix <prefix>
42                              Assume cross compilation. <prefix> should be a
43                              path to the directory where the cross
44                              compilation tools are located, plus the platform
45                              prefix, e.g. "/path/to/tools/i586-pc-haiku-".
46                              This overrides the HAIKU_* tool variables.
47  --distro-compatibility <level>
48                              The distribution's level of compatibility with
49                              the official Haiku distribution. The generated
50                              files will contain the respective trademarks
51                              accordingly.
52                              official -- the official Haiku distribution.
53                              compatible -- a Haiku Compatible (tm) distro.
54                              default -- any other distro (default value).
55  --enable-multiuser          Enable experimental multiuser support.
56  --help                      Prints out this help.
57  --host-only                 Configure for building tools for the build host
58                              only. Haiku cannot be built when configured like
59                              this.
60  --include-gpl-addons        Include GPL licensed add-ons.
61  --include-patented-code     Enable code that is known to implemented patented
62                              ideas and techniques. If this option is not
63                              specified, the resulting distribution may still
64                              implement patented ideas and techniques. This
65                              option only disables code that is currently known
66                              to be problematic.
67  --include-sources           Includes the source code of projects that require
68                              either an offer of source code or a copy of the
69                              patched sources. This is preferable when
70                              distributing on physical mediums.
71  --include-3rdparty          Include 3rdparty/ in the build system.
72  -j<n>                       Only relevant for --build-cross-tools and
73                              --build-cross-tools-gcc4. Is passed on to the
74                              make building the build tools.
75  --target=TARGET             Select build target platform.
76                              [default=${TARGET_PLATFORM}]
77                              valid targets=r5,bone,dano,haiku
78  --target-arch <arch>        Haiku only: Specify the target architecture to
79                              build for. Must be one of the architectures of the
80                              host system. The installed build tools for that
81                              architecture will be used.
82                              This option can be specified multiple times. The
83                              first occurrence specifies the primary
84                              architecture of the Haiku to build, subsequent
85                              ones the secondary architectures.
86  --update                    re-runs last configure invocation [must be given
87                              as first option!]
88  --use-gcc-pipe              Build with GCC option -pipe. Speeds up the build
89                              process, but uses more memory.
90  --use-gcc-graphite          Build with GCC Graphite engine for loop
91                              optimizations. Only for gcc 4.
92  --use-32bit                 Use -m32 flag on 64bit host gcc compiler.
93  --use-xattr                 Use Linux xattr respectively *BSD extattr support
94                              for BeOS attribute emulation. Warning: Make sure
95                              your file system supports sufficient attribute
96                              sizes (4 KB per file for all attributes won't
97                              suffice).
98  --use-xattr-ref             Use the generic BeOS attribute emulation, but use
99                              Linux xattr respectively *BSD extattr support to
100                              make it more robust (i.e. attribute mix-ups become
101                              less likely).
102
103environment variables:
104  HAIKU_AR_x86_gcc2           The static library archiver for x86_gcc2.
105                              Defaults to "ar".
106  HAIKU_CC_x86_gcc2           The x86_gcc2 compiler. Defaults to "gcc".
107  HAIKU_LD_x86_gcc2           The x86_gcc2 linker. Defaults to "ld".
108  HAIKU_OBJCOPY_x86_gcc2      The x86_gcc2 objcopy to be used. Defaults to
109                              "objcopy".
110  HAIKU_RANLIB_x86_gcc2       The static library indexer for x86_gcc2. Defaults
111                              to "ranlib".
112  HAIKU_STRIP_x86_gcc2        The x86_gcc2 strip command. Defaults to "strip".
113  HAIKU_YASM                  The yasm assembler (x86 only).
114  HAIKU_CPPFLAGS_<arch>       The preprocessor flags for target architecture
115                              <arch>. Defaults to "".
116  HAIKU_CCFLAGS_<arch>        The C flags for target architecture <arch>.
117                              Defaults to "".
118  HAIKU_CXXFLAGS_<arch>       The C++ flags for target architecture <arch>.
119                              Defaults to "".
120  HAIKU_LDFLAGS_<arch>        The linker flags for target architecture <arch>.
121                              Defaults to "".
122  HAIKU_ARFLAGS_<arch>        The flags passed to HAIKU_AR for target
123                              architecture <arch> for archiving. Defaults to
124                              "cru".
125  HAIKU_UNARFLAGS_<arch>      The flags passed to HAIKU_AR for target
126                              architecture <arch> for unarchiving. Defaults to
127                              "x".
128
129Non-default output directories:
130  By default all objects, build configuration, and other related files are
131  stored in /path/to/haiku_source/generated.  To store objects in a non-default
132  location, run "../../relative/path/to/haiku_source/configure <options>" from
133  within your non-default location.  "jam [ options ] targets" can then be run
134  directly inside your non-default location.  Another option is to invoke "jam
135  [ options ] targets" from within haiku_source.  This can be accomplished by
136  either "export HAIKU_OUTPUT_DIR=your non-default location" before invoking
137  jam or by creating a symlink of haiku_source/generated pointing to your
138  non-default location and running jam.
139
140
141EOF
142}
143
144# assertparam
145#
146# Checks whether at least one parameter is left.
147#
148assertparam()
149{
150	if [ $2 -lt 2 ]; then
151		echo $0: \`$1\': Parameter expected.
152		exit 1
153	fi
154}
155
156# assertparams
157#
158# Checks whether at least a certain number of parameters is left.
159#
160assertparams()
161{
162	if [ $3 -le $2 ]; then
163		echo $0: \`$1\': Not enough parameters.
164		exit 1
165	fi
166}
167
168# absolute_path
169#
170# returns the absolute path of a given path.
171#
172absolute_path()
173{
174	if [ "x$1" != "x${1#/}" ]; then
175		echo "$1"
176	else
177		echo "`pwd`/$1"
178	fi
179}
180
181# real_path
182#
183# returns the realpath of a symbolic link.
184#
185real_path()
186{
187	perl -MCwd=realpath -e'print realpath($ARGV[0]), "\n"' "$1"
188}
189
190# standard_gcc_settings
191#
192# Sets the variables for a GCC platform.
193#
194standard_gcc_settings()
195{
196	local gcc=$1
197
198	if which greadlink > /dev/null 2>&1; then
199		readlink="greadlink -e"
200	elif which realpath > /dev/null 2>&1; then
201		readlink=realpath
202	elif readlink -e / > /dev/null 2>&1; then
203		readlink="readlink -e"
204	else
205		readlink=real_path
206	fi
207
208	# PLATFORM_LINKLIBS
209	local gcclib=`$gcc -print-libgcc-file-name`
210	local gccdir=`dirname ${gcclib}`
211
212	local gccRawVersion=`$gcc -dumpversion`
213	local gccMachine=`$gcc -dumpmachine`
214
215	local libgcc=${gccdir}/libgcc.a
216
217	# determine architecture from machine triple
218	case $gccMachine in
219		arm-*)		targetCpu=arm;;
220		i?86-*)		targetCpu=x86;;
221		m68k-*)		targetCpu=m68k;;
222		mipsel-*)	targetCpu=mipsel;;
223		powerpc-*)	targetCpu=ppc;;
224		x86_64-*)	targetCpu=x86_64;;
225		*)
226			echo "Unsupported gcc target machine: $gccMachine" >&2
227			exit 1
228			;;
229	esac
230
231	local targetArch=$targetCpu
232	local staticLibStdCxx
233	local sharedLibStdCxx
234	local staticLibSupCxx
235	local sharedLibSupCxx
236	local kernelLibgcc
237	local cxxHeaders
238
239	case $gccRawVersion in
240		4.*)
241			# for gcc 4 we use the libstdc++ and libsupc++ that come with the
242			# compiler
243			staticLibStdCxx=`$gcc -print-file-name=libstdc++.a`
244			sharedLibStdCxx=`$gcc -print-file-name=libstdc++.so`
245			staticLibSupCxx=`$gcc -print-file-name=libsupc++.a`
246			sharedLibSupCxx=`$gcc -print-file-name=libsupc++.so`
247
248			# If the architecture has separate runtime libraries for the
249			# kernel, use them.
250			kernelLibgcc=`$gcc -print-file-name=libgcc-kernel.a`
251			if [ $kernelLibgcc = libgcc-kernel.a ]; then
252				kernelLibgcc=$libgcc
253			fi
254			kernelLibSupCxx=`$gcc -print-file-name=libsupc++-kernel.a`
255			if [ $kernelLibSupCxx = libsupc++-kernel.a ]; then
256				kernelLibSupCxx=$staticLibSupCxx
257			fi
258
259			local headersBase=$gccdir/../../../..
260			local headers=$headersBase/$gccMachine/include/c++/$gccRawVersion
261			if [ ! -d $headers ]; then
262				headers=$headersBase/include/c++/$gccRawVersion
263			fi
264
265			cxxHeaders=$headers
266			for d in $gccMachine backward ext; do
267				# Note: We need the line break, otherwise the line might become
268				# too long for jam (512 bytes max).
269				cxxHeaders="$cxxHeaders $headers/$d"
270			done
271
272			# Unset the HAIKU_{SHARED,STATIC}_LIB{STD,SUP}CXX variables, if the
273			# compiler didn't give us actual file names. Otherwise resolve
274			# symlinks to avoid problems when copying the libraries to the
275			# image.
276
277			if [ $staticLibStdCxx = libstdc++.a ]; then
278				staticLibStdCxx=
279			else
280				staticLibStdCxx=`$readlink $staticLibStdCxx`
281			fi
282
283			if [ $sharedLibStdCxx = libstdc++.so ]; then
284				sharedLibStdCxx=
285			else
286				sharedLibStdCxx=`$readlink $sharedLibStdCxx`
287			fi
288
289			if [ $staticLibSupCxx = libsupc++.a ]; then
290				staticLibSupCxx=
291			else
292				staticLibSupCxx=`$readlink $staticLibSupCxx`
293			fi
294
295			if [ $sharedLibSupCxx = libsupc++.so ]; then
296				sharedLibSupCxx=
297			else
298				sharedLibSupCxx=`$readlink $sharedLibSupCxx`
299			fi
300			;;
301		2.9*)
302			# check for correct (most up-to-date) legacy compiler and complain
303			# if an older one is installed
304			if [ $gccRawVersion != $haikuRequiredLegacyGCCVersion ]; then
305				echo "GCC version $haikuRequiredLegacyGCCVersion is required!";
306				echo "Please download it from www.haiku-os.org...";
307				exit 1;
308			fi
309
310			kernelLibgcc=$libgcc
311			kernelLibSupCxx=
312			targetArch=x86_gcc2
313			;;
314	esac
315
316	local bootLibgcc
317	local bootLibSupCxx
318	case $gccMachine in
319		x86_64-*)
320			# Boot loader is 32-bit, need the 32-bit libs.
321			bootLibgcc=`$gcc -m32 -print-libgcc-file-name`
322			bootLibSupCxx=`$gcc -m32 -print-file-name=libsupc++.a`
323			;;
324		*)
325			bootLibgcc=$libgcc
326			bootLibSupCxx=$staticLibSupCxx
327			;;
328	esac
329
330	# determine whether graphite loop optimization should/can be used
331	local useGraphite=`get_variable HAIKU_USE_GCC_GRAPHITE_$targetCpu`
332	if [ -z "$useGraphite" ]; then
333		useGraphite=$useGccGraphiteDefault
334	fi
335
336	if [ "$useGraphite" != 0 ]; then
337		UNUSED=`echo "int main() {}" | $gcc -xc -c -floop-block - 2>&1`
338		if [ $? != 0 ]; then
339			echo "GCC Graphite loop optimizations cannot be used on $targetArch"
340			useGraphite=0
341		fi
342	fi
343
344	set_variable HAIKU_CPU_$targetArch $targetCpu
345
346	get_build_tool_path CC_$targetArch "$gcc"
347	set_variable HAIKU_GCC_RAW_VERSION_$targetArch $gccRawVersion
348	set_variable HAIKU_GCC_MACHINE_$targetArch $gccMachine
349	set_variable HAIKU_GCC_LIB_DIR_$targetArch $gccdir
350	set_variable HAIKU_GCC_LIBGCC_$targetArch $libgcc
351	set_variable HAIKU_GCC_GLUE_CODE_$targetArch "crtbegin.o crtend.o"
352	set_variable HAIKU_GCC_HEADERS_DIR_$targetArch \
353		"${gccdir}/include ${gccdir}/include-fixed"
354	set_variable HAIKU_STATIC_LIBSTDCXX_$targetArch "$staticLibStdCxx"
355	set_variable HAIKU_SHARED_LIBSTDCXX_$targetArch "$sharedLibStdCxx"
356	set_variable HAIKU_STATIC_LIBSUPCXX_$targetArch "$staticLibSupCxx"
357	set_variable HAIKU_SHARED_LIBSUPCXX_$targetArch "$sharedLibSupCxx"
358	set_variable HAIKU_KERNEL_LIBSUPCXX_$targetArch "$kernelLibSupCxx"
359	set_variable HAIKU_BOOT_LIBSUPCXX_$targetArch "$bootLibSupCxx"
360	set_variable HAIKU_KERNEL_LIBGCC_$targetArch $kernelLibgcc
361	set_variable HAIKU_CXX_HEADERS_DIR_$targetArch "$cxxHeaders"
362	set_variable HAIKU_BOOT_LIBGCC_$targetArch $bootLibgcc
363	set_variable HAIKU_USE_GCC_GRAPHITE_$targetArch $useGraphite
364
365	standard_gcc_settings_targetArch=$targetArch
366}
367
368# set_variable
369#
370# Set the value of a variable.
371#
372set_variable()
373{
374	eval "$1=\"$2\""
375}
376
377# get_variable
378#
379# Echo the value of a variable.
380#
381get_variable()
382{
383	eval "echo \${$1}"
384}
385
386# set_default_value
387#
388# Set the value for a variable, if no value is set yet.
389#
390set_default_value()
391{
392	eval "$1=\${$1-$2}"
393}
394
395# get_build_tool_path
396#
397# Gets a usable absolute path of a build tool.
398#
399get_build_tool_path()
400{
401	local var="HAIKU_$1"
402	local path=$2
403
404	if [ -f "$path" ]; then
405		# get absolute path
406		local oldPwd="`pwd`"
407		cd "`dirname "$path"`"
408		path="`pwd`/`basename "$path"`"
409		cd $oldPwd
410	else
411		which "$path" > /dev/null 2>&1 || {
412			echo "Build tool \"$path\" not found." >&2
413			exit 1
414		}
415	fi
416
417	eval "$var=$path"
418}
419
420is_in_list()
421{
422	local element
423	for element in $2; do
424		if [ "$1" = "$element" ]; then
425			return 0
426		fi
427	done
428	return 1
429}
430
431# get cwd and the source directory
432currentDir=`pwd`
433cd `dirname "$0"`
434sourceDir=`pwd`
435cd "$currentDir"
436
437# backup the passed arguments
438configureArgs="$@"
439
440# internal default parameter values
441#
442platform=`uname`
443platformMachine=`uname  -m`
444targetArchs=
445buildCrossTools=
446buildCrossToolsScript="$sourceDir/build/scripts/build_cross_tools"
447buildCrossToolsJobs=
448useGccGraphiteDefault=0
449unknownArchIndex=1
450haikuTargetArchs=
451
452# exported (BuildSetup) default parameter values
453#
454HOST_GCC_RAW_VERSION=`gcc -dumpversion`
455HOST_GCC_MACHINE=`gcc -dumpmachine`
456HAIKU_INCLUDE_GPL_ADDONS=0
457HAIKU_INCLUDE_PATENTED_CODE=0
458HAIKU_INCLUDE_SOURCES=0
459HAIKU_INCLUDE_3RDPARTY=0
460HAIKU_ENABLE_MULTIUSER=0
461HAIKU_DISTRO_COMPATIBILITY=default
462TARGET_PLATFORM=haiku
463HAIKU_USE_GCC_PIPE=0
464HAIKU_HOST_USE_32BIT=0
465HAIKU_HOST_USE_XATTR=0
466HAIKU_HOST_USE_XATTR_REF=0
467HAIKU_HOST_BUILD_ONLY=0
468HOST_EXTENDED_REGEX_SED="sed -r"
469HOST_GCC_LD=`gcc -print-prog-name=ld`
470HOST_GCC_OBJCOPY=`gcc -print-prog-name=objcopy`
471SFDISK_BINARY=sfdisk
472HOST_SFDISK=$SFDISK_BINARY
473HOST_SHA256=
474HOST_HAIKU_PORTER=
475HAIKU_PORTS=
476HAIKU_PORTS_CROSS=
477
478HAIKU_PACKAGING_ARCHS=
479
480set_default_value HAIKU_YASM		yasm
481
482if sha256sum < /dev/null > /dev/null 2>&1; then
483	HOST_SHA256=sha256sum
484elif sha256 < /dev/null > /dev/null 2>&1; then
485	HOST_SHA256="sha256 -q"
486elif shasum < /dev/null > /dev/null 2>&1; then
487	HOST_SHA256="shasum -a 256"
488else
489	echo "Error: Neither sha256sum nor sha256 seem to be available!" >&2
490	exit 1
491fi
492
493haikuRequiredLegacyGCCVersion="2.95.3-haiku-2013_08_15"
494export haikuRequiredLegacyGCCVersion
495	# version of legacy gcc required to build haiku
496supportedTargetArchs="
497	arm
498	m68k
499	mipsel
500	ppc
501	x86
502	x86_64
503	x86_gcc2
504	"
505
506# determine output directory
507if [ "$currentDir" = "$sourceDir" ]; then
508	outputDir=$currentDir/generated
509else
510	outputDir=$currentDir
511fi
512buildOutputDir="$outputDir/build"
513HAIKU_BUILD_ATTRIBUTES_DIR="$outputDir/attributes"
514buildConfigFile="$buildOutputDir/BuildConfig"
515
516# check for update request
517if [ "$1" = "--update" ]; then
518	if ! [ -e "$buildConfigFile" ]; then
519		echo $0 --update: \'$buildConfigFile\' not found - updating not possible.
520		exit 1
521	fi
522	if ! type perl >/dev/null 2>&1; then
523		echo $0 --update: \'perl\' not found - updating not possible.
524		exit 1
525	fi
526	# convert BuildConfig from jam format to shell format and evaluate it
527	shellConfigFile="${buildConfigFile}.shell"
528	perl "$sourceDir/build/scripts/convert_build_config_to_shell_format.pl" \
529		<"$buildConfigFile" >"$shellConfigFile"
530	. "$shellConfigFile"
531	rm "$shellConfigFile"
532	shift
533fi
534
535# parse parameters
536#
537while [ $# -gt 0 ] ; do
538	case "$1" in
539		--bootstrap)
540			assertparams "$1" 3 $#
541			HOST_HAIKU_PORTER="`absolute_path $2`"
542			HAIKU_PORTS_CROSS="`absolute_path $3`"
543			HAIKU_PORTS="`absolute_path $4`"
544			shift 4
545			;;
546		--build-cross-tools)
547			if [ -z "$buildCrossTools" ]; then
548				assertparams "$1" 2 $#
549				targetArch=$2
550				buildCrossTools=$3
551				shift 3
552			else
553				assertparam "$1" $#
554				targetArch=$2
555				shift 2
556			fi
557			case "$targetArch" in
558				x86_gcc2)	targetMachine=i586-pc-haiku;;
559				x86)		targetMachine=i586-pc-haiku;;
560				x86_64)		targetMachine=x86_64-unknown-haiku;;
561				ppc)		targetMachine=powerpc-apple-haiku;;
562				m68k)		targetMachine=m68k-unknown-haiku;;
563				arm)		targetMachine=arm-unknown-haiku;;
564				mipsel)		targetMachine=mipsel-unknown-haiku;;
565				*)
566					echo "Unsupported target architecture: $2" >&2
567					exit 1
568					;;
569			esac
570			set_variable buildCrossToolsMachine_$targetArch $targetMachine
571			targetArchs="$targetArchs $targetArch"
572			HAIKU_PACKAGING_ARCHS=
573			;;
574		--cross-tools-prefix)
575			assertparam "$1" $#
576			targetArch=unknown${unknownArchIndex}
577			set_variable crossToolsPrefix_$targetArch "$2"
578			targetArchs="$targetArchs $targetArch"
579			HAIKU_PACKAGING_ARCHS=
580			unknownArchIndex=$(($unknownArchIndex + 1))
581			shift 2
582			;;
583		--distro-compatibility)
584			assertparam "$1" $#
585			HAIKU_DISTRO_COMPATIBILITY=$2
586			case "$HAIKU_DISTRO_COMPATIBILITY" in
587				official)	;;
588				compatible)	;;
589				default)	;;
590				*)			echo "Invalid distro compatibility" \
591								"level: $HAIKU_DISTRO_COMPATIBILITY"
592							exit 1;;
593			esac
594			shift 2
595			;;
596		--enable-multiuser)	HAIKU_ENABLE_MULTIUSER=1; shift 1;;
597		--help | -h)	usage; exit 0;;
598		--host-only)	HAIKU_HOST_BUILD_ONLY=1; shift 1;;
599		--include-gpl-addons)	HAIKU_INCLUDE_GPL_ADDONS=1; shift 1;;
600		--include-patented-code)	HAIKU_INCLUDE_PATENTED_CODE=1; shift 1;;
601		--include-sources)	HAIKU_INCLUDE_SOURCES=1; shift 1;;
602		--include-3rdparty)	HAIKU_INCLUDE_3RDPARTY=1; shift 1;;
603        -j*)				buildCrossToolsJobs="$1"; shift 1;;
604		--target=*)     TARGET_PLATFORM=`echo $1 | cut -d'=' -f2-`; shift 1;;
605		--target-arch)
606			assertparam "$1" $#
607			targetArch=$2
608			shift 2
609			if [ ! "$platform" = Haiku ]; then
610				echo "--target-arch can only be specified on Haiku." >&2
611				exit 1
612			fi
613			is_in_list "$targetArch" "$supportedTargetArchs" || (
614				echo "Unsupported target architecture: \"$targetArch\"" >&2
615				exit 1
616			)
617			haikuTargetArchs="$haikuTargetArchs $targetArch"
618			;;
619		--use-gcc-pipe)	HAIKU_USE_GCC_PIPE=1; shift 1;;
620		--use-gcc-graphite)	useGccGraphiteDefault=1; shift 1;;
621		--use-32bit)	HAIKU_HOST_USE_32BIT=1; shift 1;;
622		--use-xattr)	HAIKU_HOST_USE_XATTR=1; shift 1;;
623		--use-xattr-ref)	HAIKU_HOST_USE_XATTR_REF=1; shift 1;;
624		*)				echo Invalid argument: \`$1\'; exit 1;;
625	esac
626done
627
628# detect the build platform
629case "${platform}" in
630	Darwin)	HOST_PLATFORM=darwin ;;
631	FreeBSD)	HOST_PLATFORM=freebsd
632				SFDISK_BINARY=sfdisk-linux
633				if [ "$HAIKU_HOST_USE_32BIT" = 1 ] ; then
634					echo Unsupported platform: FreeBSD ${platformMachine}
635					exit 1
636				fi	;;
637	Haiku)	HOST_PLATFORM=haiku_host ;;
638	Linux)	HOST_PLATFORM=linux ;;
639	OpenBSD) HOST_PLATFORM=openbsd ;;
640	SunOS)	HOST_PLATFORM=sunos ;;
641	CYGWIN_NT-*) HOST_PLATFORM=cygwin ;;
642	*)		echo Unsupported platform: ${platform}
643			exit 1 ;;
644esac
645
646# check common locations for sfdisk
647for sfdiskDir in /sbin /usr/sbin /usr/local/sbin ; do
648	if [ -e ${sfdiskDir}/${SFDISK_BINARY} ]; then
649		HOST_SFDISK=${sfdiskDir}/${SFDISK_BINARY}
650	fi
651done
652
653# check for case-sensitive filesystem
654mkdir haikuCaseTest 2>/dev/null
655mkdir haikucasetest 2>/dev/null
656caseInsensitive=$?
657rmdir haikuCaseTest haikucasetest 2>/dev/null
658if [ $caseInsensitive != 0 ]; then
659	echo "You need a case-sensitive file-system to build Haiku."
660	if [ $HOST_PLATFORM = "darwin" ]; then
661		echo "You can create a case-sensitive disk image using Disk Utility and use"
662		echo "it to store the Haiku sources on."
663	fi
664	exit 1
665fi
666
667# determine how to invoke sed with extended regexp support for non-GNU sed
668if [ $HOST_PLATFORM = "darwin" ]; then
669	HOST_EXTENDED_REGEX_SED="sed -E"
670fi
671
672# create output directory
673mkdir -p "$buildOutputDir" || exit 1
674
675if [ "$HAIKU_HOST_BUILD_ONLY" = 1 ]; then
676	invalidCommand=$sourceDir/build/scripts/host_build_only
677	HAIKU_AR=$invalidCommand
678	HAIKU_CC=$invalidCommand
679	HAIKU_LD=$invalidCommand
680	HAIKU_OBJCOPY=$invalidCommand
681	HAIKU_RANLIB=$invalidCommand
682	HAIKU_ELFEDIT=$invalidCommand
683	HAIKU_YASM=$invalidCommand
684	HAIKU_STRIP=$invalidCommand
685else
686	if [ -n "$HAIKU_PACKAGING_ARCHS" ]; then
687		targetArchs="$HAIKU_PACKAGING_ARCHS"
688	fi
689	HAIKU_PACKAGING_ARCHS=
690
691	# On Haiku determine target architectures and tools automatically.
692	if [ -z "$targetArchs" ]; then
693		if [ $HOST_PLATFORM != haiku_host ]; then
694			echo "Please specify the build tools to use or build (via" \
695				"--cross-tools-prefix or --build-cross-tools) or specify a" \
696				"host-only build (--host-only)." >&2
697			exit 1
698		fi
699
700		# determine primary architecture
701		targetArch=`package list -i /system/packages/haiku-*.hpkg \
702			| sed '/^\s*architecture:/!d; s,^\s*architecture:\s*,,'`
703		is_in_list "$targetArch" "$supportedTargetArchs" || (
704			echo "Unsupported target architecture: \"$targetArch\"" >&2
705			exit 1
706		)
707		targetArchs=$targetArch
708
709		set_default_value HAIKU_AR_$targetArch			ar
710		set_default_value HAIKU_CC_$targetArch			gcc
711		set_default_value HAIKU_LD_$targetArch			ld
712		set_default_value HAIKU_OBJCOPY_$targetArch		objcopy
713		set_default_value HAIKU_RANLIB_$targetArch		ranlib
714		set_default_value HAIKU_ELFEDIT_$targetArch		elfedit
715		set_default_value HAIKU_STRIP_$targetArch		strip
716
717		# determine secondary architectures
718		for targetArch in $supportedTargetArchs; do
719			if [ -e /system/packages/haiku_$targetArch-*.hpkg ]; then
720				targetArchs="$targetArchs $targetArch"
721				set_default_value HAIKU_AR_$targetArch		ar-$targetArch
722				set_default_value HAIKU_CC_$targetArch		gcc-$targetArch
723				set_default_value HAIKU_LD_$targetArch		ld-$targetArch
724				set_default_value HAIKU_OBJCOPY_$targetArch	objcopy-$targetArch
725				set_default_value HAIKU_RANLIB_$targetArch	ranlib-$targetArch
726				set_default_value HAIKU_ELFEDIT_$targetArch	elfedit-$targetArch
727				set_default_value HAIKU_STRIP_$targetArch	strip-$targetArch
728			fi
729		done
730
731		# The target architectures might have been specified explicitly.
732		if [ -n "$haikuTargetArchs" ]; then
733			for targetArch in $haikuTargetArchs; do
734				is_in_list "$targetArch" "$targetArchs" || (
735					echo "Unsupported target architecture: \"$targetArch\"." \
736						"Only native architectures of the host platform can" \
737						"be specified." >&2
738					exit 1
739				)
740			done
741			targetArchs="$haikuTargetArchs"
742		fi
743	fi
744
745	isPrimaryArch=1
746	for targetArch in $targetArchs; do
747		# Note: targetArch is "unknown<n>" at this point, if a cross-tools
748		# prefix was specified. The standard_gcc_settings call below will get
749		# the actual architecture.
750
751		crossToolsPrefix=`get_variable crossToolsPrefix_$targetArch`
752
753		# build cross tools from sources
754		if [ -n "$buildCrossTools" -a -z "$crossToolsPrefix" ]; then
755			crossToolsDir="$outputDir/cross-tools-$targetArch"
756			targetMachine=`get_variable buildCrossToolsMachine_$targetArch`
757			script="$buildCrossToolsScript"
758			scriptArgs=
759			if [ $targetArch != x86_gcc2 ]; then
760				script="${script}_gcc4"
761				scriptArgs="$targetMachine"
762			fi
763			secondaryArch=
764			if [ -z "$isPrimaryArch" ]; then
765				secondaryArch=$targetArch
766			fi
767
768			case $HOST_PLATFORM in
769				freebsd|openbsd)	MAKE=gmake;;
770				*)					MAKE=make;;
771			esac
772
773			MAKE=$MAKE \
774			SECONDARY_ARCH=$secondaryArch \
775			HAIKU_USE_GCC_GRAPHITE=`get_variable \
776				HAIKU_USE_GCC_GRAPHITE_$targetArch` \
777			"$script" $scriptArgs "$sourceDir" "$buildCrossTools" \
778				"$crossToolsDir" $buildCrossToolsJobs || exit 1
779			crossToolsPrefix="$crossToolsDir/bin/${targetMachine}-"
780		fi
781
782		# prepare gcc settings and get the actual target architecture
783		gcc="${crossToolsPrefix}gcc"
784		if [ -z "${crossToolsPrefix}" ]; then
785			gcc=`get_variable HAIKU_CC_$targetArch`
786		fi
787		standard_gcc_settings "$gcc"
788		targetArch=$standard_gcc_settings_targetArch
789
790		# set default values for flags
791		set_default_value HAIKU_CPPFLAGS_$targetArch	""
792		set_default_value HAIKU_CCFLAGS_$targetArch		""
793		set_default_value HAIKU_CXXFLAGS_$targetArch	""
794		set_default_value HAIKU_LDFLAGS_$targetArch		""
795		set_default_value HAIKU_ARFLAGS_$targetArch		cru
796		set_default_value HAIKU_UNARFLAGS_$targetArch	x
797
798		# Override the cross tools variables, if the tools were built or a
799		# prefix was specified.
800		if [ -n "$crossToolsPrefix" ]; then
801			get_build_tool_path AR_$targetArch ${crossToolsPrefix}ar
802			get_build_tool_path LD_$targetArch ${crossToolsPrefix}ld
803			get_build_tool_path OBJCOPY_$targetArch ${crossToolsPrefix}objcopy
804			get_build_tool_path RANLIB_$targetArch ${crossToolsPrefix}ranlib
805			get_build_tool_path STRIP_$targetArch ${crossToolsPrefix}strip
806
807			case `get_variable HAIKU_GCC_RAW_VERSION_$targetArch` in
808				4.*)
809					get_build_tool_path ELFEDIT_$targetArch \
810						${crossToolsPrefix}elfedit
811				;;
812			esac
813		fi
814
815		# Get the libgcc objects. We couldn't do that in standard_gcc_settings,
816		# since we need "ar", which may be set later.
817		ar=`get_variable HAIKU_AR_$targetArch`
818		libgcc=`get_variable HAIKU_GCC_LIBGCC_$targetArch`
819		set_variable HAIKU_GCC_LIBGCC_OBJECTS_$targetArch \
820			"`$ar t $libgcc | grep -v eabi.o`"
821			# Note: We filter out eabi.o. It's present in gcc's libgcc for PPC
822			# and neither needed nor wanted.
823
824		# check whether the Haiku compiler really targets Haiku
825		targetMachine=`get_variable HAIKU_GCC_MACHINE_$targetArch`
826		case "$targetMachine" in
827			*-*-haiku)	;;
828			*)
829				echo The compiler specified as Haiku target compiler is not a \
830				valid Haiku cross-compiler. Please see ReadMe.cross-compile. >&2
831				echo compiler: $HAIKU_CC
832				echo compiler is configured for target: $targetMachine
833				exit 1 ;;
834		esac
835
836		HAIKU_PACKAGING_ARCHS="$HAIKU_PACKAGING_ARCHS $targetArch"
837		isPrimaryArch=
838	done
839fi
840
841# Generate BuildConfig
842cat << EOF > "$buildConfigFile"
843# BuildConfig
844# Note: This file has been automatically generated by configure with the
845# following arguments:
846# ${configureArgs}
847
848TARGET_PLATFORM 			?= "${TARGET_PLATFORM}" ;
849HOST_PLATFORM				?= "${HOST_PLATFORM}" ;
850
851HAIKU_INCLUDE_GPL_ADDONS			?= "${HAIKU_INCLUDE_GPL_ADDONS}" ;
852HAIKU_INCLUDE_PATENTED_CODE			?= "${HAIKU_INCLUDE_PATENTED_CODE}" ;
853HAIKU_INCLUDE_SOURCES				?= "${HAIKU_INCLUDE_SOURCES}" ;
854HAIKU_INCLUDE_3RDPARTY				?= "${HAIKU_INCLUDE_3RDPARTY}" ;
855HAIKU_ENABLE_MULTIUSER				?= "${HAIKU_ENABLE_MULTIUSER}" ;
856HAIKU_DISTRO_COMPATIBILITY			?= "${HAIKU_DISTRO_COMPATIBILITY}" ;
857HAIKU_USE_GCC_PIPE					?= "${HAIKU_USE_GCC_PIPE}" ;
858HAIKU_HOST_USE_32BIT				?= "${HAIKU_HOST_USE_32BIT}" ;
859HAIKU_HOST_USE_XATTR				?= "${HAIKU_HOST_USE_XATTR}" ;
860HAIKU_HOST_USE_XATTR_REF			?= "${HAIKU_HOST_USE_XATTR_REF}" ;
861HAIKU_HOST_BUILD_ONLY				?= "${HAIKU_HOST_BUILD_ONLY}" ;
862
863HAIKU_PACKAGING_ARCHS		?= ${HAIKU_PACKAGING_ARCHS} ;
864
865HAIKU_BUILD_ATTRIBUTES_DIR	?= ${HAIKU_BUILD_ATTRIBUTES_DIR} ;
866
867HAIKU_YASM					?= ${HAIKU_YASM} ;
868
869HOST_EXTENDED_REGEX_SED		?= ${HOST_EXTENDED_REGEX_SED} ;
870HOST_GCC_RAW_VERSION		?= ${HOST_GCC_RAW_VERSION} ;
871HOST_GCC_MACHINE			?= ${HOST_GCC_MACHINE} ;
872HOST_LD						?= ${HOST_GCC_LD} ;
873HOST_OBJCOPY				?= ${HOST_GCC_OBJCOPY} ;
874HOST_SFDISK					?= ${HOST_SFDISK} ;
875HOST_SHA256					?= ${HOST_SHA256} ;
876
877HOST_HAIKU_PORTER			?= ${HOST_HAIKU_PORTER} ;
878HAIKU_PORTS					?= ${HAIKU_PORTS} ;
879HAIKU_PORTS_CROSS			?= ${HAIKU_PORTS_CROSS} ;
880
881EOF
882
883for targetArch in $HAIKU_PACKAGING_ARCHS; do
884	variables="
885		HAIKU_GCC_RAW_VERSION		HAIKU_GCC_RAW_VERSION
886		HAIKU_GCC_MACHINE			HAIKU_GCC_MACHINE
887		HAIKU_GCC_LIB_DIR			HAIKU_GCC_LIB_DIR
888		HAIKU_GCC_LIBGCC			HAIKU_GCC_LIBGCC
889		HAIKU_CPU					HAIKU_CPU
890		HAIKU_STATIC_LIBSTDC++		HAIKU_STATIC_LIBSTDCXX
891		HAIKU_SHARED_LIBSTDC++		HAIKU_SHARED_LIBSTDCXX
892		HAIKU_STATIC_LIBSUPC++		HAIKU_STATIC_LIBSUPCXX
893		HAIKU_SHARED_LIBSUPC++		HAIKU_SHARED_LIBSUPCXX
894		HAIKU_KERNEL_LIBGCC			HAIKU_KERNEL_LIBGCC
895		HAIKU_KERNEL_LIBSUPC++		HAIKU_KERNEL_LIBSUPCXX
896		HAIKU_BOOT_LIBGCC			HAIKU_BOOT_LIBGCC
897		HAIKU_BOOT_LIBSUPC++		HAIKU_BOOT_LIBSUPCXX
898		HAIKU_AR					HAIKU_AR
899		HAIKU_CC					HAIKU_CC
900		HAIKU_LD					HAIKU_LD
901		HAIKU_OBJCOPY				HAIKU_OBJCOPY
902		HAIKU_RANLIB				HAIKU_RANLIB
903		HAIKU_ELFEDIT				HAIKU_ELFEDIT
904		HAIKU_STRIP					HAIKU_STRIP
905		HAIKU_CPPFLAGS				HAIKU_CPPFLAGS
906		HAIKU_CCFLAGS				HAIKU_CCFLAGS
907		HAIKU_C++FLAGS				HAIKU_CXXFLAGS
908		HAIKU_LDFLAGS				HAIKU_LDFLAGS
909		HAIKU_ARFLAGS				HAIKU_ARFLAGS
910		HAIKU_UNARFLAGS				HAIKU_UNARFLAGS
911		HAIKU_USE_GCC_GRAPHITE		HAIKU_USE_GCC_GRAPHITE
912		"
913	set -- $variables
914	while [ $# -ge 2 ]; do
915		value=`get_variable ${2}_$targetArch`
916		echo "${1}_${targetArch} ?= $value ;" >> "$buildConfigFile"
917		shift 2
918	done
919
920	# For variables that may have long values, distribute them over multiple
921	# lines so that jam doesn't hit the maximum line length.
922	variables="
923		HAIKU_GCC_HEADERS_DIR		HAIKU_GCC_HEADERS_DIR
924		HAIKU_C++_HEADERS_DIR		HAIKU_CXX_HEADERS_DIR
925		HAIKU_GCC_LIBGCC_OBJECTS	HAIKU_GCC_LIBGCC_OBJECTS
926		"
927	set -- $variables
928	while [ $# -ge 2 ]; do
929		echo "${1}_${targetArch} ?= " >> "$buildConfigFile"
930		get_variable ${2}_$targetArch | xargs -n 1 echo "   " \
931			>> "$buildConfigFile"
932		echo "    ;" >> "$buildConfigFile"
933		shift 2
934	done
935done
936
937
938# Generate a boot strap Jamfile in the output directory.
939
940cat << EOF > $outputDir/Jamfile
941# automatically generated Jamfile
942
943HAIKU_TOP			= ${sourceDir} ;
944HAIKU_OUTPUT_DIR	= ${outputDir} ;
945
946include [ FDirName \$(HAIKU_TOP) Jamfile ] ;
947
948EOF
949