1d0097591SIngo Weinhold#!/bin/sh
2d0097591SIngo Weinhold#
396c72d69SFrançois Revol# parameters <machine> <haiku sourcedir> <buildtools dir> <install dir>
429ef597dSIngo Weinhold
529ef597dSIngo Weinhold# get and check the parameters
660d8d8fcSIngo Weinholdif [ $# -lt 4 ]; then
760d8d8fcSIngo Weinhold	echo Usage: $0 '<machine> <haiku sourcedir> <buildtools dir>' \
8c0e8cc1aSIngo Weinhold		'<install dir>' >&2
929ef597dSIngo Weinhold	exit 1
1029ef597dSIngo Weinholdfi
1129ef597dSIngo Weinhold
12d1feb7cbSwaddlesplashset -e
13d1feb7cbSwaddlesplash
1420ab75e6SIngo WeinholdhaikuMachine=$1
1520ab75e6SIngo WeinholdhaikuSourceDir=$2
1620ab75e6SIngo WeinholdbuildToolsDir=$3
17c0e8cc1aSIngo WeinholdinstallDir=$4
1860d8d8fcSIngo Weinholdshift 4
1960d8d8fcSIngo WeinholdadditionalMakeArgs=$*
204da6cf84SFrançois RevolgdbSourceDir="$HAIKU_USE_GDB"
2129ef597dSIngo Weinhold
22b3ece905SJérôme DuvalccFlags="-O2"
23b3ece905SJérôme DuvalcxxFlags="-O2"
241c91ed03SFrançois RevolbinutilsTargets="$haikuMachine"
254da6cf84SFrançois RevolgdbTarget="$haikuMachine"
2691446a14SFrançois Revolcase $haikuMachine in
271c91ed03SFrançois Revoli586-*)
281c91ed03SFrançois Revol	binutilsConfigureArgs="--disable-multilib"
291c91ed03SFrançois Revol	gccConfigureArgs="--disable-multilib"
301c91ed03SFrançois Revol	binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe"
314da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
321c91ed03SFrançois Revol	;;
33266681e2SIngo Weinholdx86_64-*)
3481eb68f2SAlex Smith	# GCC's default is to enable multilib, but there is a bug when
3581eb68f2SAlex Smith	# explicitly using --enable-multilib that causes a build
3681eb68f2SAlex Smith	# failure
3781eb68f2SAlex Smith	binutilsConfigureArgs=""
381c91ed03SFrançois Revol	binutilsTargets="$binutilsTargets,i386-efi-pe,x86_64-efi-pe"
394da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
40266681e2SIngo Weinhold	;;
4191446a14SFrançois Revolm68k-*)
4291446a14SFrançois Revol	binutilsConfigureArgs="--enable-multilib"
4391446a14SFrançois Revol	gccConfigureArgs="--enable-multilib"
444da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
45e4091183SFrançois Revol	gdbTarget="m68k-unknown-elf"
4691446a14SFrançois Revol	;;
4791446a14SFrançois Revolarm-*)
48b8058ea8SPulkoMandy	# Multilib creates a lot of confusion as the wrong libs end up being linked.
49574dbabbSPulkoMandy	# For now, target Cortex-A8 devices.
50574dbabbSPulkoMandy	binutilsConfigureArgs="--disable-multilib --with-float=hard
51d958d336SAdrien Destugues		--with-cpu=cortex-a8 --with-fpu=vfpv3"
52574dbabbSPulkoMandy	gccConfigureArgs="--disable-multilib --with-float=hard
53d958d336SAdrien Destugues		--with-cpu=cortex-a8 --with-fpu=vfpv3"
54d1e057eeSArvind S Raj
55d1e057eeSArvind S Raj	# TODO: Disable building with TLS support for ARM until implemented.
566d345e6cSArvind S Raj	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
576d345e6cSArvind S Raj	gccConfigureArgs="$gccConfigureArgs --disable-tls"
584da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib --disable-werror -enable-interwork"
594da6cf84SFrançois Revol	gdbTarget="arm-unknown-elf"
6091446a14SFrançois Revol	;;
611c91ed03SFrançois Revolpowerpc-*)
621c91ed03SFrançois Revol	binutilsConfigureArgs="--disable-multilib"
631c91ed03SFrançois Revol	gccConfigureArgs="--disable-multilib"
641c91ed03SFrançois Revol	binutilsTargets="$binutilsTargets,powerpc-apple-linux,powerpc-apple-freebsd,powerpc-apple-vxworks"
654da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib --disable-werror"
664da6cf84SFrançois Revol	gdbTarget="powerpc-unknown-elf"
6782876dc4SAlexander von Gluck IV
6882876dc4SAlexander von Gluck IV	# TODO: Disable building with TLS support for PPC until implemented.
6982876dc4SAlexander von Gluck IV	binutilsConfigureArgs="$binutilsConfigureArgs --disable-tls"
7082876dc4SAlexander von Gluck IV	gccConfigureArgs="$gccConfigureArgs --disable-tls"
711c91ed03SFrançois Revol	;;
7291446a14SFrançois Revol*)
7391446a14SFrançois Revol	binutilsConfigureArgs="--disable-multilib"
7491446a14SFrançois Revol	gccConfigureArgs="--disable-multilib"
754da6cf84SFrançois Revol	gdbConfigureArgs="--disable-multilib"
7691446a14SFrançois Revol	;;
7791446a14SFrançois Revolesac
7891446a14SFrançois Revol
79a7e9ce67SJohn Scipioneif [ `uname -s` = 'Haiku' ]; then
80de2188e9SOliver Tappe	# force cross-build if building on Haiku:
81de2188e9SOliver Tappe	buildhostMachine=${haikuMachine}_buildhost
82de2188e9SOliver Tappe	buildHostSpec="--build=$buildhostMachine --host=$buildhostMachine"
83de2188e9SOliver Tappefi
84de2188e9SOliver Tappe
85467f4ab3SJeroen Oortwijnif [ ! -d "$haikuSourceDir" ]; then
8629ef597dSIngo Weinhold	echo "No such directory: \"$haikuSourceDir\"" >&2
8729ef597dSIngo Weinhold	exit 1
8829ef597dSIngo Weinholdfi
8929ef597dSIngo Weinhold
90467f4ab3SJeroen Oortwijnif [ ! -d "$buildToolsDir" ]; then
9129ef597dSIngo Weinhold	echo "No such directory: \"$buildToolsDir\"" >&2
9229ef597dSIngo Weinhold	exit 1
9329ef597dSIngo Weinholdfi
9429ef597dSIngo Weinhold
954da6cf84SFrançois Revolif [ -n "$gdbSourceDir" -a ! -d "$gdbSourceDir" ]; then
964da6cf84SFrançois Revol	echo "No such directory: \"$gdbSourceDir\"" >&2
974da6cf84SFrançois Revol	exit 1
984da6cf84SFrançois Revolfi
994da6cf84SFrançois Revol
10029ef597dSIngo Weinhold
10129ef597dSIngo Weinhold# create the output dir
102467f4ab3SJeroen Oortwijnmkdir -p "$installDir" || exit 1
10329ef597dSIngo Weinhold
10429ef597dSIngo Weinhold
10529ef597dSIngo Weinhold# get absolute paths
10629ef597dSIngo WeinholdcurrentDir=$(pwd)
10729ef597dSIngo Weinhold
108467f4ab3SJeroen Oortwijncd "$haikuSourceDir"
10929ef597dSIngo WeinholdhaikuSourceDir=$(pwd)
110467f4ab3SJeroen Oortwijncd "$currentDir"
11129ef597dSIngo Weinhold
112467f4ab3SJeroen Oortwijncd "$buildToolsDir"
11329ef597dSIngo WeinholdbuildToolsDir=$(pwd)
114467f4ab3SJeroen Oortwijncd "$currentDir"
11529ef597dSIngo Weinhold
116467f4ab3SJeroen Oortwijncd "$installDir"
117c0e8cc1aSIngo WeinholdinstallDir=$(pwd)
118467f4ab3SJeroen Oortwijncd "$currentDir"
11929ef597dSIngo Weinhold
1204da6cf84SFrançois Revolif [ -n "$gdbSourceDir" ]; then
1214da6cf84SFrançois Revolcd "$gdbSourceDir"
1224da6cf84SFrançois RevolgdbSourceDir=$(pwd)
1234da6cf84SFrançois Revolcd "$currentDir"
1244da6cf84SFrançois Revolfi
1254da6cf84SFrançois Revol
126467f4ab3SJeroen OortwijnbinutilsSourceDir="$buildToolsDir/binutils"
127467f4ab3SJeroen OortwijngccSourceDir="$buildToolsDir/gcc"
12829ef597dSIngo Weinhold
12929ef597dSIngo Weinhold
13029ef597dSIngo Weinhold# get gcc version
131467f4ab3SJeroen OortwijngccVersion=$(cat "$gccSourceDir/gcc/BASE-VER")
13229ef597dSIngo Weinhold
13329ef597dSIngo Weinholdif [ -z "$gccVersion" ]; then
13429ef597dSIngo Weinhold	echo "Failed to find out gcc version." >&2
13529ef597dSIngo Weinhold	exit 1
13629ef597dSIngo Weinholdfi
13729ef597dSIngo Weinhold
1385a7156f4SAugustin Cavalier# clear out the "missing" scripts so that they don't cause errors on tools
1395a7156f4SAugustin Cavalier# that are not installed, as we will have committed any files generated by
1405a7156f4SAugustin Cavalier# them to the buildtools repo already.
1415a7156f4SAugustin Cavalierecho "#!/bin/sh" >"$binutilsSourceDir"/missing
1425a7156f4SAugustin Cavalierecho "#!/bin/sh" >"$gccSourceDir"/missing
1435a7156f4SAugustin Cavalierecho "#!/bin/sh" >"$gccSourceDir"/isl/missing
144d8b0a135SAugustin Cavalier
14529ef597dSIngo Weinhold# create the object and installation directories for the cross compilation tools
146467f4ab3SJeroen OortwijnobjDir="${installDir}-build"
147467f4ab3SJeroen OortwijnbinutilsObjDir="$objDir/binutils"
148467f4ab3SJeroen OortwijngccObjDir="$objDir/gcc"
1494da6cf84SFrançois RevolgdbObjDir="$objDir/gdb"
150467f4ab3SJeroen OortwijnstdcxxObjDir="$objDir/stdcxx"
151467f4ab3SJeroen OortwijnsysrootDir="$installDir/sysroot"
152467f4ab3SJeroen OortwijntmpIncludeDir="$sysrootDir/boot/system/develop/headers"
153467f4ab3SJeroen OortwijntmpLibDir="$sysrootDir/boot/system/develop/lib"
15429ef597dSIngo Weinhold
155467f4ab3SJeroen Oortwijnrm -rf "$installDir" "$objDir"
15629ef597dSIngo Weinhold
1574da6cf84SFrançois Revolmkdir -p "$installDir" "$objDir" "$binutilsObjDir" "$gccObjDir" "$gdbObjDir" \
1584da6cf84SFrançois Revol	"$stdcxxObjDir" "$tmpIncludeDir" "$tmpLibDir" || exit 1
159467f4ab3SJeroen Oortwijnmkdir -p "$installDir/lib/gcc/$haikuMachine/$gccVersion"
16020ab75e6SIngo Weinhold
1610837d6c6SJerome Duvalif [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
162467f4ab3SJeroen Oortwijn	cloogSourceDir="$buildToolsDir/cloog"
163467f4ab3SJeroen Oortwijn	gmpSourceDir="$buildToolsDir/gcc/gmp"
1641e93288eSJérôme Duval	islSourceDir="$buildToolsDir/isl"
165467f4ab3SJeroen Oortwijn
1661e93288eSJérôme Duval	islObjDir="$objDir/isl"
167467f4ab3SJeroen Oortwijn	gmpObjDir="$objDir/gmp"
168467f4ab3SJeroen Oortwijn	cloogObjDir="$objDir/cloog"
1691e93288eSJérôme Duval	mkdir -p "$islObjDir" "$gmpObjDir" "$cloogObjDir" || exit 1
170467f4ab3SJeroen Oortwijn
1711e93288eSJérôme Duval	gccConfigureArgs="$gccConfigureArgs --with-cloog=$installDir \
1721e93288eSJérôme Duval		--enable-cloog-backend=isl --with-isl=$installDir \
1731e93288eSJérôme Duval		--with-gmp=$installDir \
174575cfdf2SJérôme Duval		--with-host-libstdcxx=\"-lstdc++\""
1750837d6c6SJerome Duvalfi
176b3ece905SJérôme Duvalif [ "$HAIKU_USE_GCC_PIPE" = 1 ]; then
177b3ece905SJérôme Duval	ccFlags="$ccFlags -pipe"
178b3ece905SJérôme Duval	cxxFlags="$cxxFlags -pipe"
179b3ece905SJérôme Duvalfi
1800837d6c6SJerome Duval
18168ccc989SIngo Weinholdif [ -n "$SECONDARY_ARCH" ]; then
182394c099fSIngo Weinhold	gccConfigureArgs="$gccConfigureArgs --with-hybrid-secondary=$SECONDARY_ARCH"
18368ccc989SIngo Weinholdfi
18468ccc989SIngo Weinhold
185d0064875SOliver Tappe# force the POSIX locale, as the build (makeinfo) might choke otherwise
186d0064875SOliver Tappeexport LC_ALL=POSIX
18729ef597dSIngo Weinhold
1884da6cf84SFrançois Revol# build gdb
1894da6cf84SFrançois Revolif [ -n "$HAIKU_USE_GDB" ]; then
1904da6cf84SFrançois Revolcd "$gdbObjDir"
1914da6cf84SFrançois Revol	"$gdbSourceDir/configure" \
1924da6cf84SFrançois Revol		--prefix="$installDir" --target=$gdbTarget \
1934da6cf84SFrançois Revol		$gdbConfigureArgs \
1944da6cf84SFrançois Revol		|| exit 1
1954da6cf84SFrançois Revol	$MAKE $additionalMakeArgs || exit 1
1964da6cf84SFrançois Revol	$MAKE $additionalMakeArgs install || exit 1
1974da6cf84SFrançois Revolfi
1984da6cf84SFrançois Revol
19929ef597dSIngo Weinhold# build binutils
200467f4ab3SJeroen Oortwijncd "$binutilsObjDir"
201b3ece905SJérôme DuvalCFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$binutilsSourceDir/configure" \
202467f4ab3SJeroen Oortwijn	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
2031c91ed03SFrançois Revol	--enable-targets=$binutilsTargets \
204de2188e9SOliver Tappe	--disable-nls --disable-shared --disable-werror \
205849f5810SAugustin Cavalier	--with-sysroot="$sysrootDir" \
20675f7932aSwaddlesplash	--disable-maintainer-mode \
207de2188e9SOliver Tappe	$binutilsConfigureArgs \
208de2188e9SOliver Tappe	|| exit 1
20960d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs || exit 1
21060d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs install || exit 1
21129ef597dSIngo Weinhold
2124dd5af2fSAugustin Cavalierexport PATH="$PATH:$installDir/bin"
21329ef597dSIngo Weinhold
2140837d6c6SJerome Duvalif [ "$HAIKU_USE_GCC_GRAPHITE" = 1 ]; then
215e0d19800SJerome Duval	# build gmp
216467f4ab3SJeroen Oortwijn	cd "$gmpObjDir"
217b3ece905SJérôme Duval	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gmpSourceDir/configure" \
218b3ece905SJérôme Duval		--prefix="$installDir" --disable-shared --enable-cxx || exit 1
219e0d19800SJerome Duval	$MAKE $additionalMakeArgs || exit 1
220e0d19800SJerome Duval	$MAKE $additionalMakeArgs install || exit 1
221f0aee2b4SFredrik Holmqvist
2221e93288eSJérôme Duval	# build isl
2231e93288eSJérôme Duval	cd "$islObjDir"
224b3ece905SJérôme Duval	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$islSourceDir/configure" \
2251e93288eSJérôme Duval		--prefix="$installDir" --disable-nls --disable-shared \
2261e93288eSJérôme Duval		 --with-gmp-prefix="$installDir" || exit 1
2271e93288eSJérôme Duval	$MAKE $additionalMakeArgs || exit 1
2281e93288eSJérôme Duval	$MAKE $additionalMakeArgs install || exit 1
2290837d6c6SJerome Duval
2300837d6c6SJerome Duval	# build cloog
231467f4ab3SJeroen Oortwijn	cd "$cloogObjDir"
232b3ece905SJérôme Duval	CFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$cloogSourceDir/configure" \
233467f4ab3SJeroen Oortwijn		--prefix="$installDir" --disable-nls --disable-shared \
2341e93288eSJérôme Duval		--with-gmp-prefix="$installDir" --with-isl-prefix=="$installDir" \
2351e93288eSJérôme Duval		|| exit 1
2360837d6c6SJerome Duval	$MAKE $additionalMakeArgs || exit 1
2370837d6c6SJerome Duval	$MAKE $additionalMakeArgs install || exit 1
2380837d6c6SJerome Duvalfi
23929ef597dSIngo Weinhold
24029ef597dSIngo Weinhold# build gcc
24129ef597dSIngo Weinhold
24229ef597dSIngo Weinhold# prepare the include files
24329ef597dSIngo Weinholdcopy_headers()
24429ef597dSIngo Weinhold{
24529ef597dSIngo Weinhold	sourceDir=$1
24629ef597dSIngo Weinhold	targetDir=$2
24729ef597dSIngo Weinhold
2484107ac9cSJérôme Duval	headers="$(find $sourceDir -name \*\.h)"
24929ef597dSIngo Weinhold	headers="$(echo $headers | sed -e s@$sourceDir/@@g)"
25029ef597dSIngo Weinhold	for f in $headers; do
251467f4ab3SJeroen Oortwijn		headerTargetDir="$targetDir/$(dirname $f)"
252467f4ab3SJeroen Oortwijn		mkdir -p "$headerTargetDir"
253467f4ab3SJeroen Oortwijn		cp "$sourceDir/$f" "$headerTargetDir"
25429ef597dSIngo Weinhold	done
25529ef597dSIngo Weinhold}
25629ef597dSIngo Weinhold
257467f4ab3SJeroen Oortwijncopy_headers "$haikuSourceDir/headers/config" "$tmpIncludeDir/config"
258467f4ab3SJeroen Oortwijncopy_headers "$haikuSourceDir/headers/os" "$tmpIncludeDir/os"
259467f4ab3SJeroen Oortwijncopy_headers "$haikuSourceDir/headers/posix" "$tmpIncludeDir/posix"
26029ef597dSIngo Weinhold
26129ef597dSIngo Weinhold# configure gcc
262467f4ab3SJeroen Oortwijncd "$gccObjDir"
263b3ece905SJérôme DuvalCFLAGS="$ccFlags" CXXFLAGS="$cxxFlags" "$gccSourceDir/configure" \
264467f4ab3SJeroen Oortwijn	--prefix="$installDir" $buildHostSpec --target=$haikuMachine \
265de2188e9SOliver Tappe	--disable-nls --disable-shared --with-system-zlib \
266c59b279bSJérôme Duval	--enable-languages=c,c++ --enable-lto --enable-frame-pointer \
2676cc6f047SJessica Hamilton	--enable-__cxa-atexit --enable-threads=posix \
2686cc6f047SJessica Hamilton	--with-default-libstdcxx-abi=gcc4-compatible \
2696cc6f047SJessica Hamilton	--with-sysroot="$sysrootDir" \
27075f7932aSwaddlesplash	--disable-maintainer-mode \
271de2188e9SOliver Tappe	$gccConfigureArgs \
272de2188e9SOliver Tappe	|| exit 1
27329ef597dSIngo Weinhold
27429ef597dSIngo Weinhold# make gcc
27560d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs || {
27629ef597dSIngo Weinhold	echo "ERROR: Building gcc failed." >&2
27729ef597dSIngo Weinhold	exit 1
27829ef597dSIngo Weinhold}
27929ef597dSIngo Weinhold
28029ef597dSIngo Weinhold# install gcc
28160d8d8fcSIngo Weinhold$MAKE $additionalMakeArgs install || {
28229ef597dSIngo Weinhold	echo "ERROR: Installing the cross compiler failed." >&2
28329ef597dSIngo Weinhold	exit 1
28429ef597dSIngo Weinhold}
28529ef597dSIngo Weinhold
28611ae3c5fSOliver Tappecase $haikuMachine in
28711ae3c5fSOliver Tappex86_64-*)
28811ae3c5fSOliver Tappe	# pick up the 32-bit libraries for the bootloader
28911ae3c5fSOliver Tappe	bootLibgcc=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libgcc.a`
29011ae3c5fSOliver Tappe	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibgcc
29111ae3c5fSOliver Tappe	bootLibsupcxx=`$installDir/bin/$haikuMachine-gcc -m32 -print-file-name=libsupc++.a`
29211ae3c5fSOliver Tappe	$installDir/bin/$haikuMachine-strip --strip-debug $bootLibsupcxx
29311ae3c5fSOliver Tappe	;;
29411ae3c5fSOliver Tappeesac
295c864ba1aSAlex Smith
29629ef597dSIngo Weinhold# cleanup
29729ef597dSIngo Weinhold
29829ef597dSIngo Weinhold# remove the system headers from the installation dir
29929ef597dSIngo Weinhold# Only the ones from the source tree should be used.
300467f4ab3SJeroen Oortwijnrm -rf "$installDir/$haikuMachine/sys-include"
30129ef597dSIngo Weinhold
302d511533aSAugustin Cavalier# rename the static libstdc++ to prevent accidental usage
303d511533aSAugustin Cavalier# (it should be used by the bootstrap process only)
304d511533aSAugustin Cavaliermv "$installDir/$haikuMachine/lib/libstdc++.a" \
305d511533aSAugustin Cavalier	"$installDir/$haikuMachine/lib/libstdc++-static.a"
3069bb9cc88SAugustin Cavalier
307f975dc63SIngo Weinhold# remove the sysroot dir
308467f4ab3SJeroen Oortwijnrm -rf "$sysrootDir"
309f975dc63SIngo Weinhold
31029ef597dSIngo Weinhold# remove the objects dir
311467f4ab3SJeroen Oortwijnrm -rf "$objDir"
31229ef597dSIngo Weinhold
31329ef597dSIngo Weinhold
31429ef597dSIngo Weinholdecho "binutils and gcc for cross compilation have been built successfully!"
315