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